0

I have an action common/location

#encoding: utf-8 
class CommonController < ApplicationController
  def location
    @locations=Location.select(['`key`','name','parent'])
    render json: @locations
  end
end

I want to include this js through:

=javascript_include_tag "common/location"

but this dosen't work!

So I want to know how to include a js from an ation.

4
  • Is it json, not javascript? Even if you could include it like that, what good would it be? Commented Oct 25, 2013 at 7:46
  • javascript_include_tag is used to include a javascript file, not to render json code in your views. Commented Oct 25, 2013 at 7:47
  • =javascript_include_tag url_for(action:'location',controller:'common') Commented Oct 25, 2013 at 7:49
  • you are right,I get the js from an ajax get request,and assignment the data to an variable. Commented Oct 25, 2013 at 8:01

1 Answer 1

1

If you want your locations in javascript you can do several things:

  • Use gon
  • write javascript code in your view
  • use ajax to fetch your locations (http://yourhost.com/common/location.js)
  • use html5 data attributes to set your locations in the views
  • probably other ways

Take a look at http://railscasts.com/episodes/324-passing-data-to-javascript

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.