Search code examples
ruby-on-rails-3viewpartialsrenderpartial

Correct way to share a view in the index page


I'm a Ruby-on-Rails newbie, just starting out.

I have an MVC called "account_types", generated via scaffold to produce:

  • controllers/account_types_controller.rb
  • helpers/account_types_helper.rb
  • models/account_type.rb
  • views/account_types/_form, edit, index etc...

Going to localhost:3000/account_types gives me the index view.

What I'd like to do is display the same data as selected from the account_types index method in the application index page as a list.

I wrote a new view called account_types/_list.html_erb as follows:

<ul>
<% @account_types.each do |account| %>
    <li><% account.label %></li>
<% end %>
</ul>

I then edited home/index.html.erb (This is based on examples given in other questions on SO):

<%= render :partial => 'account_types/list', :module_types => @module_types %>

However I get

undefined method `each' for nil:NilClass

and the error displays the code from account_types/_list.html.erb where I've written

<% @account_types.each do |account| %>

The scaffolded views work fine, why aren't mine?

Are partials the right thing to use here?

Thanks in advance.


Solution

  • What is the correct way to define an application-wide partial and its variables in rails says to use a before_filter on ApplicationController.