actually i have done somethign similiar these is my code
<%= @todo.todostags.build %>
<%= form_for(@todo) do |f| %>
<div class="actions">
<%= f.submit %>
</div>
here the todo model
class Todo < ActiveRecord::Base
has_many :todostags, :foreign_key =>"todos_id" , :primary_key =>'id'
accepts_nested_attributes_for :todostags, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? {|k,v| v.blank?}}
end
class Todostag < ActiveRecord::Base
belongs_to :tag , :foreign_key =>"tags_id"
belongs_to :todo , :foreign_key =>"todos_id"
end
On Tue, Mar 1, 2011 at 10:49 AM, Fearless Fool <[email protected]> wrote:
> Frederick Cheung wrote in post #984616:
>> That sounds like you've got an unsaved metered_service somewhere (eg
>> by doing premise.metered_services.build)
>> Fred
>
> Yep, that's true. I based my code on the canonical blog posts /
> comments example, so in views/premises/edit.html.erb, I wrote:
>
> <%= form_for([@premise,
> @premise.metered_services.build(:account_identifier => "gas")]) do |f|
> %>
> <%= f.submit "add gas account" %>
> <% end %>
>
> ... and it clearly calls build() whenever it renders the page. But this
> leads to two questions:
>
> * (Curiosity:) How did render(... :collection) know about the newly
> created metered_service without an ID? I know that
> premise.metered_services.build will create a new record, but it appears
> that the new record doesn't appear on the premises.metered_services list
> until it's saved.
>
> * (Necessity:) Assume the user is looking at
> views/premises/edit.html.erb. I want a button that says "add
> metered_service to the current premise". What's the right way to do
> that? (Clearly, I haven't made friends with form_for() just yet.)
>
> TIA.
>
> - ff
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> 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.
>
>
--
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.