3

I tried different ways to retrieve json-data unsuccessfully

my order.js.coffee

 $.ajax(
  url: "/orders/", // want "/orders/1"  
  dataType: "JSON"    
 ).done (data) ->
   console.log data.status

my controller:

class OrdersController < ApplicationController
respond_to :json, :js, :html


def show
  @order = Order.find(params[:id])
  respond_with @order
end
....

my show.json.jbuilder

json.extract! @order, :id, :status

and here my route:

get '/orders/:id', to: 'orders#show', defaults: {format: :json}
resources :orders, only: [:new, :create]
...

I got a lot of errors with different approach.

Started GET "/orders/" for 127.0.0.1 at 2014-10-07 01:07:36 +0200

ActionController::RoutingError (No route matches [GET] "/orders"):

or

ERROR bad URI `/orders/%3C%=%20order.id%20%%3E'
12
  • Where do you expect to get your id from in your CoffeeScript? Is your order.js.coffee supposed to be order.js.coffee.erb perhaps? Commented Oct 7, 2014 at 0:54
  • why would you expect to 1) be able to evaluate erb in you coffeescript that is loaded outside the context of the controller and 2) expect that making a get request to the index action would do anything when you expressly limit the orders action to new and create actions in the controller? Commented Oct 7, 2014 at 11:07
  • @JedSchneider my fault, I want to get request to show action not to the index, but I don't know how :( I want a request like: url: "/orders/1 where 1 is a parameter that I want to pass from the show action Commented Oct 7, 2014 at 11:56
  • $.get('/orders/1') Commented Oct 7, 2014 at 17:25
  • @JedSchneider I did not explain myself well. Example: url: "/orders/1 but 1 is a parameter that change at every new order. In other word I need something like this: /orders/:id, thanks for your time. Commented Oct 7, 2014 at 17:46

1 Answer 1

3

I've found one solution, maybe not the better solution but it works...

in order.js.coffee

id = $('#status_id').attr('data-id')
$.ajax
url: "/orders/#{id}"
type: "GET"
success: (data) ->    
  console.log(data)

in the partial:

<div id="status_id" data-id="<%= @order.id %>">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.