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'
user3832640
  • 85
  • 1
  • 9
  • 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? – mu is too short Oct 07 '14 at 00: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? – Jed Schneider Oct 07 '14 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 – user3832640 Oct 07 '14 at 11:56
  • `$.get('/orders/1')` – Jed Schneider Oct 07 '14 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. – user3832640 Oct 07 '14 at 17:46
  • from the index action, or the show action? – Jed Schneider Oct 07 '14 at 19:02
  • @JedSchneider show action, there isn't index action. – user3832640 Oct 07 '14 at 20:07
  • why would you render a show view, and then fetch the data via ajax? – Jed Schneider Oct 07 '14 at 22:18
  • if, for whatever reason you'd want to do that, your id is already in the url of the page you're on. so, just pull it out via js and make an ajax request with that id. – Jed Schneider Oct 07 '14 at 22:19
  • @JedSchneider again sorry, my english is too bad. I need to fetch data from the root. It is a single page application without other action.. – user3832640 Oct 08 '14 at 08:42
  • then again we are back to not having an index action in order to fetch the orders in the first place. voting to close this question. try asking something more specific. you have several problems here 1) you don't understand routing, 2) you don't understand ajax, 3) you tangentially don't understand rails or jQuery. all that is OK, that is why SO exists. but if you break the problem down and search around, you will see these questions have already been answered. – Jed Schneider Oct 08 '14 at 11:31
  • I don't understand because you insist with the index action, I need the "id" and "id" came from show action. Maybe my question is not clear and I'm sorry for this. Anyway thanks for your time I found one solution to pass the id and I update later my question with the solution. – user3832640 Oct 08 '14 at 15:05

1 Answers1

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 %>">
user3832640
  • 85
  • 1
  • 9