undefined method for nil:NilClass in my Rails app - ruby-on-rails-3

I'm getting a:
undefined method `lat' for nil:NilClass
error and I want to solve it. To be exact the message is:
NoMethodError in Static_pages#faq
Showing /home/christophecompaq/Pop/app/views/layouts/_signed_in_header.html.erb where line #101 raised:
undefined method `lat' for nil:NilClass
Extracted source (around line #101):
98: <div id="javascript_tags with lat and lang">
99: <%= javascript_tag do %>
100: user_latitude = "<%=raw #user.lat %>";
101: user_longitude = "<%=raw #user.lng %>";
102:
103: <% end %>
104: </div>
I have 2 other pages in the Static Pages folder where I also get this error - 'Contact Us' and 'About Us'.
But when I take the code below out of my _signed_in_header.html.erb and paste it in my file called show.html.erb, the errors stop.
<div id="javascript_tags with lat and lang">
<%= javascript_tag do %>
user_latitude = "<%=raw #user.lat %>";
user_longitude = "<%=raw #user.lng %>";
<% end %>
</div>
But the thing is, I need to have that code in the _signed_in_header.html.erb.
So I suppose my question is, 'where can I define user_latitude = "<%=raw #user.lat %>" and user_longitude = "<%=raw #user.lng %>" so my pages will recognise them without giving me errors?' I've tried the application controller, the static pages controller, but I'm still getting problems. Maybe I have to put something in the routes file, I'm not sure? Thanks for any help.

You have to look into the controller method where you defined the #user instance variable -- probably Static_pages#faq.
What's happening is that #user is equal to nil when you passed it to the view. So when you try to send it the lat method, that generates the undefined methodlat' for nil:NilClass` error.

Related

will_paginate -ActionView::Template::Error variable appears to be empty

I'm trying to implement a simple index page for one on my models that uses the will_paginate gem. (Rails 3) and am receiving the following error:
ActionView::Template::Error (The #load_verifications variable appears to be empty. Did you forget to pass the collection object for will_paginate?)
My code is the same (as far as I can tell) as all the other index pages I'm using will_paginate on that work.
LoadVerificationsController.rb:
def index
#loadVerifications = LoadVerification.paginate(page: params[:page], per_page: 10).order('ship_date')
end
index.html.erb:
<% provide(:title, 'All Loads')%>
<h1>All Loads</h1>
<%= will_paginate %>
<ul>
<%= render #loadVerifications %>
</ul>
<%= will_paginate %>
_load_verification.html.erb:
<li>
<%= loadVerification.sales_order %>
<%= loadVerification.ship_date %>
<%= loadVerification.pallet_count %>
<%= link_to "view", loadVerification %>
Any help would be appreciated. I'm not sure where the #load_verification variable referenced in the error message comes from since I haven't declared it in the controller and the variable I'm using is #loadVerification (no underscore). I will also mention that I don't currently have any data in the LoadVerifications table, but I would think that will_paginate is smart enough to handle an empty result set without throwing exceptions.
Thanks!
As official will_paginate gem documentation says: you should pass your collection (#loadVerifications in your case) as a parameter to will_paginate view helper:
<%= will_paginate #loadVerifications %>
Here's the source code which raises this error.
method call from within will_paginate method.
method declaration.
Briefly: if no collection is given to will_paginate helper, it tries to build instance variable's name from controller's name

undefined method `each' for nil:NilClass in Rails while using a for loop

Getting an undefined method `each' for nil:NilClass error for
<% for movie in trailers %>
<li>
<article>
<div class="photo">
<img src="<%= movie['thumbnail'] %>" alt="Photo" class="wrap_me_white"/>
</div>
<div class="details">
<h4 class="title"></h4>
</div>
</article>
</li>
<% end %>
This is using an API from rotten tomatoes which is defined and working properly for all the other areas using different APIs.
def trailers
config = Rails.application.config
resp = HTTParty.get("#{config.rotten_tomatoes_api_url}movies/770672122/clips.json?apikey=#{config.rotten_tomatoes_api_key}")
movies = JSON.parse(resp.body)['movies']
end
I have checked to make sure all the fields are correct according to rotten tomatoes. I am not sure what to do next as I've I've gone through all the problems and fixes that I have used previously. I really appreciate your time, and if there is more data needed let me know and I'd be more than happy to supply it. Thanks!
For the method trailers you are sending the response JSON.parse(resp.body)['movies']
if it returns an array then the loop will works correctly. If it returns nil then you will get the error
undefined method 'each' for nil:NilClass
because you are trying to iterate a nil object.
better modify the loop like this,
<% #trailers = trailers %>
<% for movie in #trailers %>
<li> ... </li>
<% end if #trailers.present? %>
Try using an instance variable in the controller action so that the view can display each of the movies:
#movies = JSON.parse(resp.body)['movies']
Then, in your view, use the each method instead of a for loop:
<% #movies.each do |movie| %>
...
<% end %>

No Route Matches error for defined route

I'm having an issue with Rails, I get the following error:
No route matches {:action=>"publish", :controller=>"businesses"}
The offending code:
<div id="searchDatesDiv" style="margin: 0 auto;">
<%= form_tag(publish_business_path, :method => :post) do %>
<%= submit_tag("Publish") %>
<% end %>
</div>
routes.rb:
resources :businesses do
member do
post 'publish'
end
end
rake routes:
registration GET /registrations/:id(.:format) registrations#show
publish_business POST /businesses/:id/publish(.:format) businesses#publish
businesses GET /businesses(.:format) businesses#index
POST /businesses(.:format) businesses#create
I can see the path defined in rake routes. Why am I getting this error? The form is also a POST method. Any help would be great! I've tried looking at similar questions on SO but haven't found one that works in my case :(.
Your business publish path requires an id parameter that you are not giving it. Pass the business object to the path helper thusly:
<%= form_tag(publish_business_path(#business), :method => :post) do %>

Rails 3.2 app: undefined method `klass' for nil:NilClass for a Class that exists and renders on page just fine

I am running a pretty straight forward Rails 3.2 app on Dreamhost (I know its lame).
The error is:
NoMethodError in Products#edit
Showing /home/emeraldcityguitars/emeraldcityguitars.com/releases/20120318040051/app/views/products/edit.html.erb where line #47 raised:
undefined method `klass' for nil:NilClass
Extracted source (around line #47):
44: <div id="interior_sub_head"><h5 id="sub_green_head"><%= #product.title %></h5></div>
45: <div id="interior_pad">
46:
47: <%= nested_form_for #product, :html => { :multipart => true } do |f| %>
48: <%= f.error_messages %>
49:
50: <fieldset>
It sounds like #product is returning nil, but if you look at line 44, it didn't error when requesting #product in that case?
any ideas?
EDIT
Full Product#edit view: https://gist.github.com/2081340
+
_photo_fields.html view: https://gist.github.com/5af2f88f29c21e24b42c
Thanks
I had the same problem and I made two changes, unfortunately not separately, and one or both solved the problem.
First, in your controller do you build a photo in your edit method?
#product.photos.build if #product.photos.empty?
I found that in the comments on this blog post.
Second, I added a symbol for the nested class to the 'link_to_add' line, which you already have.
I was switching from a has_many to has_one and was getting this same error. Azolo's response fixed my issue.
Removed the <%= f.link_to_add......%> tag and all is working nicely!

Preview button causes an error in my code

It use to work great under rails 2 and now moving to rails 3 as caused my code to have errors.
Error Message:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Slice of code:
<div id="archive-list">
<h4>Archives</h4>
<ul>
<% #archive_list.each do |item| -%>
<li><%= link_to(item[0], archive_url(:year => item[1], :month => item[2])) %></li>
<% end -%>
</ul>
there seems to be an issue with:
<% #archive_list.each do |item| -%>
with this line above
Any help would be great?
#archive_list is probably nil
do a puts #archive_list in the controller before rendering the view and check if it's nil
in the view you could do a <% if #archive_list.exists? %> before iterating over it, if it's an active record relation, or <% if #archive_list.blank? %> if it's something else (from the looks of the code it's the latter).