0

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.

Hellfire
  • 25
  • 4

3 Answers3

1

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

Community
  • 1
  • 1
Hellfire
  • 25
  • 4
0

You pass :module_types to partial, but use account_types. As I can see you just need to change your index.html.erb to:

<%= render :partial => 'account_types/list', :account_types => @module_types %>
Donotello
  • 496
  • 5
  • 12
  • Sorry, as above I've confused everyone with module_types - that was something else I was trying - it should have been account_types. `<%= render :partial => 'account_types/list', :account_types => @account_types %> still gives the same error message. – Hellfire Apr 30 '12 at 13:29
  • I've created a new project to recreate this problem. I've set everything up as before. If I don't add (amp)account_types = AccountType.all to home_controller.rb, I get the same error about nil.each If I add (amp)account_types = AccountType.all to home_controller.rb then I get an li for each record, but no lists of data (which is strange in itself). However, (amp)account_types is already defined in account_types_controller.rb, so in the interests of DRY, can I somehow use the index controller from account_types instead of having to rewrite it in the home controller to use it in the home view? – Hellfire May 02 '12 at 13:43
  • 1
    I think I have it working now. http://stackoverflow.com/questions/8869291/what-is-the-correct-way-to-define-an-application-wide-partial-and-its-variables says to use a `before_filter` on `ApplicationController`. I've put that in, and suddenly the `account_types` variable is available to every controller. This is exactly what I needed. – Hellfire May 03 '12 at 21:53
0

You can use partials for this if you want, though it would be unnecessary in this case as far as I can tell (they are for sharing chunks of code between several views). In order to get this code to work you'll need to define @account_types in your controller with something like

@account_types = AccountType.all

You can see exact line in your account_types_controller.rb under index action. :module_types => @module_types is not necessary here, since I doubt you defined @module_types either and you don't use module_types in your partial at all.

It's obvious, that you don't understand how Rails works, so I suggest reading through a good tutorial (like this one) before you proceed with whatever you have in mind.

TsukinoMai
  • 417
  • 2
  • 10
  • Oops. module_types was something else I was trying. It was supposed to be account_types. Sorry. I've got `@account_types = AccountType.all` in the controller. The trouble is that my custom view can't seem to access the variable @account_types, so it is complaining that it's nil. How do I get my custom view to access @account_types the same way `AccountTypesController.index` works? – Hellfire Apr 30 '12 at 13:22
  • Well, you can forcefully pass it as with `:account_types => @account_types`, but then you'll need to change `@account_types` in your partial to `account_types`. – TsukinoMai Apr 30 '12 at 13:37