On 23 July 2013 08:23, Colin Law <clanlaw@googlemail.com> wrote:
> On 21 July 2013 18:28, James Gray <zaargy@gmail.com> wrote:
>> Hi all,
>>
>> In my controller I am doing the following to populate a nested form for a
>> has_many through association:
>>
>> def new
>> @specification = Specification.new
>>
>> Component.find_each.each do |component|
>> @specification.component_specifications.build(:component_id =>
>> component.id)
>
> Don't use .new or .build in the view, do it in the controller so that
> everything is prepared before rendering the view. The view code
> should only be concerned with displaying the data.
Sorry, I seem to have suffered from brain fade when I wrote the above
code, it is in the controller. You might want to put the second part
in a method of the Specification model however. I am not sure why you
are using find_each but that is a different issue.
>
>> end
>>
>> The idea being whenever someone creates or edits a form, it will be
>> populated with all components at that time so that when they save those
>> components that specification will be associated with the newly created
>> specification. The problem I am having is I can't work out how to pass
>> component name to display in my form for the as yet nonexistent
>> component_specification as it is not accessible through the
>> ComponentSpecification model.
>
> I have no idea what you mean by the last sentence. But as I said
> above you should be doing this in the controller anyway.
I still don't understand what the problem you are describing is here
though. Why can't you just say
@specification.component_specifications?
Colin
>
>>
>> My models:
>>
>> class Specification < ActiveRecord::Base
>
> You said in the title ActiveModel, (well actually you said activemodel
> wihch would be something different again) but I see you meant
> ActiveRecord. When asking questions it is important to get the
> details right.
>
> Colin
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtpFHRvStXCpt4%2B9ojWamQp9106x1wvABKZjerVLPQnxw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment