Hi,

On May 12, 9:43 am, Guitaronin <[email protected]> wrote:
> The problem is that using fields_for generates form fields for each
> existing record, and for one new record.

The 'new record' is behaviour I'm unaware of! I think that you have to
build the new record in the association.

> Does anybody know how to do this? Code is below.
>
> Models:
> class MessageThread < ActiveRecord::Base
>   has_many :messages
>   accepts_nested_attributes_for :messages, :allow_destroy => true
> end
>
> class Message < ActiveRecord::Base
>   belongs_to :message_threads
> end
>
> View (show.html.erb)
>
>   <% @message_thread.messages.each do |message| %>
>   <%= message.message %>
>   <% end %>
>
> <% form_for @message_thread do |thread_form| -%>
>   <% thread_form.fields_for :messages do |message| %>
>   <%= message.label :message %>
>   <%= message.text_field :message %>
>   <% end %>

Nested attributes fields_for supports setting an explicit object as
the second parameter, like so:
<% thread_form.fields_for(:messages, Message.new) do |message| %>

Although the proper way would be to set @new_message or similar in the
controller and pass that, or thread_form.object.messages.build().

Hope that helps,
Andrew
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to