I have a form used to create a 'Style'. The style may have any number of
'features' which correspond to 'Feature' models in the features table. I
would like to add the appropriate entries to a relationship table
('stylefeatures') whenever a Style is created. Here is a breakdown of my
models:

class Style < ActiveRecord::Base

has_many :stylefeatures

has_many :features, :through => :stylefeatures, :foreign_key => :feature_id

accepts_nested_attributes_for :stylefeatures
end

class Feature < ActiveRecord::Base

has_many :stylefeatures

has_many :styles, :through => :stylefeatures, :foreign_key => :style_id
end

class Stylefeature < ActiveRecord::Base

belongs_to :style

belongs_to :feature

accepts_nested_attributes_for :features
end

...and here is my controller for the 'new' action:

def new

@style = Style.new

@features = Feature.all

end

...and here is my form:

<%= simple_form_for @style, :html => { :class => 'form-horizontal' } do |m| %>

<%= m.simple_fields_for :features do |p| %>

<%= p.input :name, :label => "Features", :collection => @features,
:input_html => { :multiple => true } %>

<% end %>

[....]
<% end %>

Right, so the list of features DOES load up into the multiple select box
within my form. So that's sweet. However, when I go to submit I receive the
error: No association found for name 'features'. Has it been defined yet?

I get the exact same error when adding @style.features.build to the
controller 'new' action.

This is what the params hash looks like. It carries the ID of the feature
name selected.

"features"=>{"name"=>["","7"]}

So my question is, how do I go about loading the features listed in the
features table for the nested form, and on form submission, handle the
selected features for addition to the stylefeatures relationship table.

Thanks very much

--
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?hl=en.

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 4 | next ›
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMay 20, '12 at 8:01p
activeMay 21, '12 at 10:46a
posts4
users3
websiterubyonrails.org
irc#RubyOnRails

3 users in discussion

Abram: 2 posts Vishal singh: 1 post Colin Law: 1 post

People

Translate

site design / logo © 2021 Grokbase