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
Related
I'm creating a blog and on the index I'm looping through a partial for each article so I can show the post without a comments section, it looks like this:
<% render partial: 'view_post_only', collection: #articles %>
Which takes you to the partial _view_post_only:
<h1><%= link_to #article.title, article_path %></h1>
<p>
Written by <%= link_to #article.author.username, author_path(#article.author_id), class: "article-listing" %>
</p>
<% if #article.image.exists? %>
<p><%= image_tag #article.image.url %></p>
<% end %>
<p class="article-body"><%= #article.body %></p>
I'm getting this error on the first line of view_post_only:
No route matches {:action=>"show", :controller=>"articles"} missing required keys: [:id]
and lastly, included in my routes.db is:
resources :articles do
resources :comments
end
To resolve your first error, change:
<h1><%= link_to #article.title, article_path %></h1>
To:
<h1><%= link_to #article.title, article_path(#article) %></h1>
The article_path helper needs an instance of the resource you're attempting to link to so it knows where to go.
Also, the instance of each of your "articles" is not saved in the #article variable. By convention rails will singularize the partial's name and give you a method to access the item. In this case that convention would yield a method called view_post_only (I think). So you could replace #article with view_post_only in your template. Like so:
<h1><%= link_to view_post_only.title, article_path(view_post_only) %></h1>
This convention doesn't seem to work well in this case. To make your partial read better I'd be explicit about what you want to call the item:
<% render partial: 'view_post_only', collection: #articles, as: :article %>
and then replace your references to #article with just article in _view_post_only.
I'm new to rails, and I'm trying to figure out how to make the [action] attribute on a form dynamic so that I can reuse the form markup.
In MVC.net it's easy as you usually specify the :controller and :action.
However in rails there's just a "simple_form_for(#my_model)" method.
If I browse to /my_models/new the action attribute is:
action = "/my_models"
But if I go to /my_models/1/edit the action attribute is:
action = "my_models/1"
What if I want to create a new action for handling POSTs of my_model AND still reuse the same _form.html.erb... how does that work?
I think it's actually rails doing this
https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/form_helper.rb#L230
In short it just infers the url based on the resource and whether it is new or an existing one.
SimpleForm's FormBuilder is inherited from ActionView::Helpers::FormBuilder
You can check out that code https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/form_builder.rb
I guess I'm not understanding the last part. Say you wanted to use a custom action instead of the inferred ones you could just make a partial of the form elements and use that partial for all forms.
so _form.html.erb
<%= simple_form_for #my_object do |f| %>
<%= render 'form_elements' %>
<% end %>
_custom_form.html.erb
<%= simple_form_for #my_object, :url => custom_url do |f| %>
<%= render 'form_elements' %>
<% end %>
_form_elements.html.erb
form elements as usual
I have a partial which generates a div with some form fields in it. It uses the form builder variable "f" which is provided as input to correctly name the fields in the parameter has (fields are actually nested attributes, so the name is like "[author][book][0][title]").
I want to use that same partial when receiving an AJAX call to regenerate the div based on new user information. I am currently using <% form_for ... |f| %> in my erb file, but that generates a warning that "<% %>" is deprecated.
My erb file looks like the following:
<% if f.nil? %>
<% form_for(#author, :id => :coupon_form) do |f| %>
<%= render "books_detail1", :f => f %>
<% end %>
<% else %>
<%= render "books_detail1", :f => f %>
<% end %>
So what is the correct way to create a form builder context while discarding the generated HTML?
The correct answer is to use fields_for. It generates the same form builder object without the html. I lost track of this in it's use for sub-forms, but it's really the same thing.
I'm coding the project from Ruby on Rails Tutorial: Learn Rails by Example and am having trouble with the following and unfollowing features.
I have a piece of HTML in one of my pages that looks like this:
<%= form_for current_user.relationships.build(:followed_id => #user.id),
:remote => true do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<div class="actions"><%= f.submit "Follow" %></div>
<% end %>
My RelationshipsController has a create method, but it is never called. The same applies to my 'unfollow' html and corresponding destroy method. Is there something I need to add to my project to let Rails know that the relationships.build method should call the create method, or is that automatic?
Thanks in advance.
RelationshipsController has a create method
current_user.relationships.build()
Is it create or build?
Show us the controller code. Since you use "remote=> true" you probably need to change the "respond_to" code in there and create a js.erb file.
I am developing a facebook app using rails 3 and facebooker2
i cannot find any example on how to use fb_server_fbml helper method.
what is "proc" in its paramter. Can anyone provide me with sample code?
Thanks
The &proc parameter is used as a content block that is inserted between the <fb:serverFbml> tags. You can learn more about block helpers and differences between Rails 2.3 and Rails 3 here: Block Helpers in Rails 3
So, try something like this:
<% fb_server_fbml do %>
<%#Insert here your content %>
<% end %>
If you want to use the request form, try the following:
<% fb_server_fbml do %>
<% fb_request_form("Your app name","http://www.example.com/callback","Try this out!") do %>
<%= fb_multi_friend_selector("Invite your friends:", {:rows => 3}) %>
<% end %>
<% end %>