Hi everyone, I have a partial containing a few input elements to edit the properties of a vehicle. Those input fields are of course within a form, but they are not the only ones in there. Now when my page loads, I want to display all vehicles already added by the current user. So I thought I'd prepare a collection of appropriate vehicles in @vehicles and let the view render the vehicles using <% if @vehicles %> <%= render :partial => "vehicle", :collection => @vehicles %> <% end %> The thing is, as I need to display more than one vehicle "form", I need to give each input field a reference to the id of the underlying activercord. I'm trying this using <%= text_field 'vehicle[]', 'acquisition_cost', { :class => "autosave_object", :"tmt:required" => "true", :"tmt:filters" => "numbersonly", :size => 5, :tabindex => 4 } %> As said in AWDWR the [] after the instance variable name adds the id to the name attribute of the textfield. Unfortunately, there is no instance variable called @vehicle in my controller since I only have the collection @vehicles. I get the following error You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occured while evaluating nil.id_before_type_cast Of course there is a local variable called vehicle. I can use it to manually build the necessary name attribute for the html input field. However, the input field is NOT connected to any instance variable, since there simply is none to connect to :( Any ideas how to approach this? cheers Martin Gamsjäger --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Is this dirty ^^ =? i = 1 @vehicles.each do |vehicle| instance_eval("@vehicle_#{i} = vehicle") end anyway it works :) On 9/18/06, Martin Gamsjaeger <gamsnjaga@gmail.com> wrote:> > Hi everyone, > > I have a partial containing a few input elements to edit the properties of > a vehicle. Those input fields are of course within a form, but they are not > the only ones in there. Now when my page loads, I want to display all > vehicles already added by the current user. So I thought I'd prepare a > collection of appropriate vehicles in @vehicles and let the view render the > vehicles using > > <% if @vehicles %> > <%= render :partial => "vehicle", :collection => @vehicles %> > <% end %> > > The thing is, as I need to display more than one vehicle "form", I need to > give each input field a reference to the id of the underlying activercord. > I'm trying this using > > <%= text_field 'vehicle[]', 'acquisition_cost', { :class => > "autosave_object", :"tmt:required" => "true", :"tmt:filters" => > "numbersonly", :size => 5, :tabindex => 4 } %> > > As said in AWDWR the [] after the instance variable name adds the id to > the name attribute of the textfield. Unfortunately, there is no instance > variable called @vehicle in my controller since I only have the collection > @vehicles. I get the following error > > You have a nil object when you didn't expect it! > You might have expected an instance of ActiveRecord::Base. > The error occured while evaluating nil.id_before_type_cast > > Of course there is a local variable called vehicle. I can use it to > manually build the necessary name attribute for the html input field. > However, the input field is NOT connected to any instance variable, since > there simply is none to connect to :( > > Any ideas how to approach this? > > cheers > Martin Gamsjäger > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 9/18/06, Martin Gamsjaeger <gamsnjaga-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Is this dirty ^^ =? > > i = 1 > @vehicles.each do |vehicle| > instance_eval("@vehicle_#{i} = vehicle") > end > > anyway it works :) >Ohh yeah.. That''s dirty ;) there is a method called each_with_index @vehicles.each_with_index do |vehicle, index| instance_eval( "@vechile_#{index} = vehicle" ) end But in this situation you could, setup an instance variable in your partial. <% @vehicle = vehicle %> I don''t know that it''s any less dirty though. Good Luck --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Daniel, Thx for your tip! I will go with your solution <% @vehicle = vehicle %>. This seems the way to go for me, as I can avoid one of evals family members ^^ cheers Martin On 9/18/06, Daniel N <has.sox-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On 9/18/06, Martin Gamsjaeger <gamsnjaga-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Is this dirty ^^ =? > > > > i = 1 > > @vehicles.each do |vehicle| > > instance_eval("@vehicle_#{i} = vehicle") > > end > > > > anyway it works :) > > > > Ohh yeah.. That''s dirty ;) > > there is a method called each_with_index > > @vehicles.each_with_index do |vehicle, index| > instance_eval( "@vechile_#{index} = vehicle" ) > end > > But in this situation you could, setup an instance variable in your > partial. > > <% @vehicle = vehicle %> > > I don''t know that it''s any less dirty though. > > Good Luck > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---