In my assets, I have a file called maps.js.erb
, basically with only the following (debugging) content:
alert("<%= @map.width %>");
This JS file is loaded through the show.html.erb
view belonging to maps.
<%= javascript_include_tag params[:controller] %>
<h1><%= @map.title %></h1>
…
The HTML file itself shows the map's title, e.g. when browsing to /maps/1/
, since @map
is defined in the controller. However, as soon as I include the JS file, I get the following error:
NoMethodError in Maps#show
Showing
…/app/views/maps/show.html.erb
where line #1 raised:undefined method 'title' for nil:NilClass (in
…/app/assets/javascripts/maps.js.erb
)
- Why is
@map
not available in thejs.erb
file? - How else can I access this instance variable defined in my controller?