
fields_for in Rails not working with one-to-many associations
Reported by yves.senn (at gmail) | June 13th, 2009 @ 05:49 AM
The ActionView::Helpers::FormHelper#fields_for method in Rails 2.3 allows you to create forms with nested models. This does not work with DataMapper and the one-to-many association. It seems, that it always uses one-to-one associations. The example below illustrated the problem:
Models:
class Artist
include DataMapper::Resource
property :id, Serial
has n, :profiles
end
class Profile
include DataMapper::Resource
property :id, Integer, :serial => true
property :data, String
property :adapter, String
belongs_to :artist
end
View:
<% form.fields_for :profiles do |p| %>
<p><label for=""><%= p.object.adapter %></label>
<%= p.text_field :data %>
</p>
<% end %>
Generated field:
<input id="artist_profiles_data" name="artist[profiles][data]" size="30" type="text" />
Expected field:
<input id="artist_profiles_0_data" name="artist[profiles][0][data]" size="30" type="text" />
Comments and changes to this ticket
-
Martin Gamsjaeger (snusnu) June 26th, 2009 @ 08:35 AM
- State cleared.
This kind of functionality isn't provided by dm-core. Also, I thought that even activerecord needs a call like
accepts_nested_attributes_for :profiles
in the Artist definition to achieve the above?
You can have a look at http://github.com/snusnu/dm-accepts_nested_attributes/tree/master which aims to provide this functionality for datamapper. I wouldn't expect that to work with the #fields_for helper out of the box though.
If you give the plugin a shot, feel free to contact me about any issues you have with it. It would be nice to provide the same end user experience rails/activerecord users enjoy now, if possible.
-
Dan Kubb (dkubb) June 27th, 2009 @ 03:55 AM
- State set to not-applicable
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »