# Newbie Method Error Question

**URL:** <https://www.ruby-forum.com/t/newbie-method-error-question/204066>\
**Category:** Rails\
**Created:** [March 4, 2011, 9:13am UTC](https://www.ruby-forum.com/t/newbie-method-error-question/204066 "2011-03-04T09:13:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 4, 2011, 9:13am UTC](https://www.ruby-forum.com/t/newbie-method-error-question/204066/1 "2011-03-04T09:13:38Z")

</div>

Hi Rails group,

I understand the basic CRUD scaffold setup. I’m trying to figure out  
the best way to implement the default index behavior with a model on  
every page as navigation. I’m having a conflict implementing both  
List.all and List.new on the same page.

For example, in create new, a basic call to the controller would  
include:  
\<%= form\_for @list do |form| %\>  
name:

\<%= form.text\_field :name %\>

\<%= form.submit %\>

 \<% end %\>

The controller is:  
def new  
@list = List.new  
end

The problem I have is how to also show the List.all on the same page.  
The DEF index uses @lists = List.all for the function. I can’t invoke  
@list on the same page to both equal List.new and List.all.

Is the best practice to create a different method in the controller  
(set it as a before\_filter so I can show on all views) to show all?

I’ve created the following to test it out at in the controller:  
before\_filter :nav\_show\_lists

def nav\_show\_lists  
respond\_with(@nav\_lists = List.all)  
end

Then it is invoked in the application.html.erb view:  
\<% @nav\_lists.each do |list| %\>

- \<%= link\_to “#{list.name}”, list\_url(list) %\>
  
\<% end %\>

Thanks

---

<div class="post-metadata">

**Author:** ![rex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f1d935/32.png) [@rex](https://www.ruby-forum.com/u/rex)\
**Post date:** [March 5, 2011, 2:50am UTC](https://www.ruby-forum.com/t/newbie-method-error-question/204066/2 "2011-03-05T02:50:13Z")

</div>

This applies to all newbies: It’s important to get your words right. If  
you’re not going to do this, or you can’t, then use more words rather  
than less and show actual code. Sentences like “a model on every page  
for navigation” don’t make sense. Use your own words rather than  
technical ones. It’s always better.

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)  
Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
New video up now at [http://sensei.zenunit.com/](http://sensei.zenunit.com/) real fastcgi rails deploy  
process! Check it out now!

---

<div class="post-metadata">

**Author:** ![rex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f1d935/32.png) [@rex](https://www.ruby-forum.com/u/rex)\
**Post date:** [March 4, 2011, 3:45pm UTC](https://www.ruby-forum.com/t/newbie-method-error-question/204066/3 "2011-03-04T15:45:50Z")

</div>

Rex,

You could set @lists and @list in the new action (they are separate  
vars), even though you stated you couldn’t. However, if I understand  
what you’re trying to achieve (assign @lists for all views) then a  
before filter is fine. You can place it in a specific controller or the  
application controller (so it’s available for all pages in your app, as  
it sounds like it’s used in Navigation).

You should think about caching, so you’re not hitting the DB each time  
an action is called. There are caching schemes out there to look into.

before\_filter :nav\_show\_lists

def nav\_show\_lists  
@nav\_lists = List.all  
end

Brian

---

<div class="post-metadata">

**Author:** ![rex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f1d935/32.png) [@rex](https://www.ruby-forum.com/u/rex)\
**Post date:** [March 5, 2011, 3:46am UTC](https://www.ruby-forum.com/t/newbie-method-error-question/204066/4 "2011-03-05T03:46:27Z")

</div>

Brian,  
Instead of using the respond\_with, @nav\_lists = List.all works. Thanks

Julian,  
Thanks for the tip. You are right about not mixing up technical terms.

---

<div class="post-metadata">

**Author:** ![rex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f1d935/32.png) [@rex](https://www.ruby-forum.com/u/rex)\
**Post date:** [March 5, 2011, 9:01pm UTC](https://www.ruby-forum.com/t/newbie-method-error-question/204066/5 "2011-03-05T21:01:40Z")

</div>

On Mar 4, 2011, at 8:48 PM, Julian L. wrote:

> This applies to all newbies: It’s important to get your words right.  
> If you’re not going to do this, or you can’t, then use more words  
> rather than less and show actual code. Sentences like “a model on  
> every page for navigation” don’t make sense. Use your own words  
> rather than technical ones. It’s always better.

I agree with this in principle, but it’s important to remember that a  
lot of our “technical” words have real-life definitions: a model may  
be found in a hobby store, or on a catwalk… (I’m too sexy for your  
Rails, too sexy…)

🕶

Walter
