开发者

Accessing a Ruby on Rails object in a Google Maps javascript

I am trying to access a field in a Ruby on Rails object from my view inside some JavaScript. Here is the relevant code in my view pick.html.erb:

var directionDisplay;

var start = <%= @route.from %>;

var end = <%= @route.to %>;

From and to are text fields that hold zipcodes. Here is my relevant Route controller code:

class RoutesController < ApplicationController

# GET /routes/1/pick

def pick

@routes = Route.find(params[:id])

end

I read somewhere that I need to use ActiveSupport::JSON in order to access an object field...Do I need to do this instead? If so, how do I install it and could you possibly give me an example on how to get started? I've been searching for examples everywhere the past few days and I can't find any.

Let me know if you need any more code and thank y开发者_StackOverflowou in advance for any reply!


The file name should be (according to convention) pick.js.erb and not pick.html.erb based on the fact that you have JavaScript code included.

If you want to include a partial inside this one, you can use the escape_javascript helper to render the partial.

If you have more than one possible file to be rendered from the pick action, you should look into using respond_to such as:

@route = Route.find(params[:id])
respond_to do |format|
  format.html
  format.js { render :js => @route }
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜