Hi there, it''s kind of a weird problem. I have a Person model and Person controller. In the new method in my controller I try to retrieve a collection of nationalities. The nationalities are stored in a different table. So retrieving those should be like: def new @person = Person.new @person.name = Name.new @person.birthday = Date.today @person.nationality = Nationality.new nationalities = Nationality.find( :all ) end Unfortunately when Rails try to access the array in the new.rhtml it''s nil. Here, I create a selection list, as follows: <p> <% form_for :gender do |form| %> <%= form.select( :gender, @genders ) %> <% end %> </p> The weird thing happens when I retrieve the array inside new.rhtml. It''s working here. <p> <% form_for :nationality do |form| %> <% @nationalities = Nationality.find( :all ) form.select( :nationality, @nationalities ) %> <% end %> </p> As stated in the new Web Development book those code should normally reside in the controller. Am I missing something? Thanks ahead, Christian --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Try using an instance variable (@nationalities) in the controller instead of a local variable (nationalities). Instance variables are available in the views, but local variables created in the controller method are not. Jason On Tue, 23 Jan 2007 17:29:04 -0000 "chhenning" <chhenning-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi there, it''s kind of a weird problem. I have a Person model and > Person controller. In the new method in my controller I try to > retrieve a collection of nationalities. The nationalities are stored > in a different table. So retrieving those should be like: > > def new > @person = Person.new > @person.name = Name.new > @person.birthday = Date.today > @person.nationality = Nationality.new > nationalities = Nationality.find( :all ) > end > > Unfortunately when Rails try to access the array in the new.rhtml it''s > nil. Here, I create a selection list, as follows: > > <p> > <% form_for :gender do |form| %> > <%= form.select( :gender, @genders ) %> > <% end %> > </p> > > > The weird thing happens when I retrieve the array inside new.rhtml. > It''s working here. > > <p> > <% form_for :nationality do |form| %> > <%> @nationalities = Nationality.find( :all ) > form.select( :nationality, @nationalities ) > %> > <% end %> > </p> > > As stated in the new Web Development book those code should normally > reside in the controller. > > Am I missing something? > > Thanks ahead, > Christian > > > >-- Jason Stewart | Tel: 616-532-2300 Systems Administrator/ | Fax: 616-532-3461 Programmer | Email: jstewart-hPVRf1w6JnE@public.gmane.org Right to Life of Michigan | Web: http://www.rtl.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Jason, I''m coming from C++ and though I''m probably more used to C++ way of compiler errors. If an variables is not accessible in the current scope why not just state that. But I guess Ruby is on a lot of ways different to C++. Anyway thanks, that was the solution. Christian On Jan 23, 12:57 pm, Jason Stewart <jstew...-hPVRf1w6JnE@public.gmane.org> wrote:> Try using an instance variable (@nationalities) in the controller > instead of a local variable (nationalities). Instance variables are > available in the views, but local variables created in the controller > method are not. > > Jason > > On Tue, 23 Jan 2007 17:29:04 -0000 > > > > "chhenning" <chhenn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi there, it''s kind of a weird problem. I have a Person model and > > Person controller. In the new method in my controller I try to > > retrieve a collection of nationalities. The nationalities are stored > > in a different table. So retrieving those should be like: > > > def new > > @person = Person.new > > @person.name = Name.new > > @person.birthday = Date.today > > @person.nationality = Nationality.new > > nationalities = Nationality.find( :all ) > > end > > > Unfortunately when Rails try to access the array in the new.rhtml it''s > > nil. Here, I create a selection list, as follows: > > > <p> > > <% form_for :gender do |form| %> > > <%= form.select( :gender, @genders ) %> > > <% end %> > > </p> > > > The weird thing happens when I retrieve the array inside new.rhtml. > > It''s working here. > > > <p> > > <% form_for :nationality do |form| %> > > <%> > @nationalities = Nationality.find( :all ) > > form.select( :nationality, @nationalities ) > > %> > > <% end %> > > </p> > > > As stated in the new Web Development book those code should normally > > reside in the controller. > > > Am I missing something? > > > Thanks ahead, > > Christian-- > Jason Stewart | Tel: 616-532-2300 > Systems Administrator/ | Fax: 616-532-3461 > Programmer | Email: jstew...-hPVRf1w6JnE@public.gmane.org > Right to Life of Michigan | Web:http://www.rtl.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---