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).
Related
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
I'm looking for a way to include a specific element at a certain position inside an iteration
Not experienced enough to use the right pattern (hence to search here on stack overflow with the right keywords, afraid of getting some duplicate question with this one)… but the base idea would as the following :
<% Post.all.each do |post| %>
<% if Post.all.index(post) == 5 # or any position %>
# render some html element (some kind of exception)
<% else %>
<%= post.title %>
<% end %>
<% end %>
But just without skipping any records in my post array
I'm not sure I have totally understood your request. each_with_index may help you, and if you don't use the else, you won't skip any records :
<% Post.all.each_with_index do |post, index| %>
<% if index == 5 # or any position %>
# render some html element (some kind of exception)
<% end %>
<%= post.title %>
<% end %>
I am having trouble with this as I get started with Ruby On Rails. Any help would be great. Additional info:
Extracted source (around line #25):
</header>
<div class="container">
<% flash.each do |key, value| %>
<%= content_tag(div, value, :class=> "alert alert-#{key}") %>
<% end %>
<%= yield %>
</div>
The line should be <%= content_tag(:div, value, :class=> "alert alert-#{key}") %>
Notice I'm passing in a symbol :div instead of just div. The error just means its trying to look for a variable or method named div but content tag accepts a symbol or string I believe. I think it will work with "div" as well.
On my web app I have a login page which is in the Pages Controller called welcome.html.erb
Inside it has a simple form for login and a simple for for sign up, both on the same page.
If the user signs up with wrong credentials (like password confirmation wrong, or length of password and etc) the controller that handles this is the new method in the Users Controller.
Inside the new method it checks if a user is created, and if not I'd like it to return to the welcome method in Pages Controller, passing to it the errors that were created during the user's creation.
It seems that if I do a
redirect_to root_path
The error count of the signup is reset when returning to the root page. If instead I call some other action of the users controller I do see the errors.
But since all of the html code is in the welcome view I don't want to replicate the code in the users controller views as well..
Is there a way to pass that errors data to the pages controller?
The partial for the error is
<% if object.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(object.errors.count, "error") %> prohibited this <%= object.class.to_s.underscore.humanize.downcase %> from being saved:</h2>
<p>There were problems with the following fields:</p>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Which I call using
<%= render 'shared/error_messages', :object => f.object %>
redirect_to welcome_path
(Or whatever the actual path is.)
From the Users controller I called
flash[:errors]=#user.errors
and then I changed the partial to be
<% if object.any? %>
<div id="error_explanation">
<h2><%= pluralize(object.count, "error") %> prohibited this <%= object.class.to_s.underscore.humanize.downcase %> from being saved:</h2>
<p>There were problems with the following fields:</p>
<ul>
<% object.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Which I called using
<%= render 'shared/error_messages', :object =>#user_errors %>
That solved the issue for now..
This is going to be a really dumb question, and I almost hate myself for asking it, but here goes.
When I run my Cucumber test, I'm getting a "syntax error, unexpected ')'" with the following code:
inside my user model:
def member?(gallery)
array = []
self.groups.each do |group|
array << group.id
end
if array.include?(gallery.group.id)
true
end
end
And in my view:
<ul>
<% #galleries.each do |gallery| %>
<% if current_user.member?(gallery) %>
<li>
<%= link_to gallery.title, gallery %>
</li>
<% end %>
<% end %>
</ul>
EDIT: Here is the important part of the error in full:
~/Coding/Rails/galleryTest/app/views/galleries/index.html.erb:8: syntax error, unexpected ')', expecting keyword_then or ';' or '\n'
... current_user.member? gallery );#output_buffer.safe_concat('
... ^
~/Coding/Rails/galleryTest/app/views/galleries/index.html.erb:13: syntax error, unexpected keyword_end, expecting ')'
'); end
^
EDIT 2: Here is the error when removing the '=':
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id (ActionView::Template::Error)
./app/models/user.rb:18:in `member?'
I have tried a few different things, and I've got to be missing something really trivial. Another pair of eyes would be greatly appreciated.
Thank you very much.
<%= if current_user.member?(gallery) %>
should be:
<% if current_user.member?(gallery) %>
Not that there is no =, it means output and your code is trying to output the response of the if block.
Alright, here is what I have, and how it seems to be working:
In my user model:
def member?(gallery)
array = self.groups.collect { |g| g.id }
if array.include?(gallery.group_id)
true
end
end
In my view:
<% if user_signed_in? %>
<ul id="private_galleries">
<% #galleries.each do |gallery| %>
<% if current_user.member?(gallery) %>
<li>
<%= link_to gallery.title, gallery %>
</li>
<% end %>
<% end %>
</ul>
<% end %>
<ul>
<% #galleries.each do |gallery| %>
<% if gallery.group_id == nil %>
<li>
<%= link_to gallery.title, gallery %>
</li>
<% end %>
<% end %>
</ul>
My tests are running alright now, but they aren't passing, which is strange, as when I set up users with groups and galleries with groups and view them with the site running, they seem to be showing appropriately, which just means my tests are probably effed. That'll be a task for the morning, and probably another question on StackOverflow!