views:

315

answers:

0

Hello, I don't understand the workaround with the virtuell attribute. Form Railsforum by Duplex URL: http://railsforum.com/viewtopic.php?pid=110698#p110698 His solution:

> def type_helper self.type end

def type_helper=(type) self.type = type end < My application is based on RailsCast part 73,74,75 complex_forms

I have two models: rohstoffhauptgruppen and rohstoffuntergruppen in a has_many , belongs_to relation. I have a virtuell attribut called :"untergruppen_zeigen" in my model "rohstoffuntergruppen" rohstoffuntergruppen.rb:

> def untergruppen_zeigen=(untergruppen_zeigen) untergruppen_zeigen.each do |zeigen| if attributes[:id].blank? rohstoffuntergruppen.build(zeigen) else rohstoffuntergruppe = rohstoffuntergruppe.detect { |t| t.id == attributes[:id].to_i } rohstoffuntergruppe.attributes = attributes end end end <

and a attr_accessible

> attr_accessible :id, :rohstoffhauptgruppenname, :untergruppen_zeigen <<

in my rohstoffhauptgruppen_controller I have

> def create @rohstoffhauptgruppe = Rohstoffhauptgruppe.new(params[:rohstoffhauptgruppe]) @rohstoffuntergruppe = @rohstoffhauptgruppe.rohstoffuntergruppen.build(params[:untergruppen_zeigen])<

if @rohstoffhauptgruppe.save flash[:notice] = "Hauptgruppe wurde gespeichert." redirect_to rohstoffhauptgruppen_path else flash[:error] = "Hauptgruppe wurde nicht gespeichtert." render :action => :new end end

> def update @rohstoffhauptgruppe = Rohstoffhauptgruppe.find(params[:id]) respond_to do |format| if @rohstoffhauptgruppe.update_attributes(params[:rohstoffhauptgruppe]) flash[:notice] = "Hauptgruppe wurde gespeichert." format.html {redirect_to rohstoffhauptgruppen_path} else flash[:error] = "Hauptgruppe wurde nicht gespeichtert." format.html {redirect_to rohstoffhauptgruppen_path} end end <

The create action works fine, only the update action raise this:

the params_array looks like this: Parameters: {"commit"=>"Speichern", "authenticity_token"=>"p8Q0O0m4lFxArr0rTbo 3P8s1LJ65t6fCiJn1OMtMd2g=", "id"=>"1", "rohstoffhauptgruppe"=>{"rohstoffhauptgru ppenname"=>"Backwaren", "untergruppen_zeigen"=>[{"rohstoffuntergruppenname"=>"Br ot", "id"=>"1"}, {"rohstoffuntergruppenname"=>"Br├Âtchen", "id"=>"2"}, {"rohstof funtergruppenname"=>"Spezialbrot", "id"=>"3"}, {"rohstoffuntergruppenname"=>"Kuc hen", "id"=>"4"}, {"rohstoffuntergruppenname"=>"Keks", "id"=>"5"}, {"rohstoffunt ergruppenname"=>"Sonstige", "id"=>"6"}]}} ←[4;36;1mRohstoffhauptgruppe Columns (20.0ms)←[0m ←[0;1mSHOW FIELDS FROM ro hstoffhauptgruppen←[0m ←[4;35;1mRohstoffhauptgruppe Load (10.0ms)←[0m ←[0mSELECT * FROM rohstoffha uptgruppen WHERE (rohstoffhauptgruppen.id = 1) ←[0m ←[4;36;1mRohstoffuntergruppe Columns (10.0ms)←[0m ←[0;1mSHOW FIELDS FROM ro hstoffuntergruppen←[0m WARNING: Can't mass-assign these protected attributes: id WARNING: Can't mass-assign these protected attributes: id WARNING: Can't mass-assign these protected attributes: id WARNING: Can't mass-assign these protected attributes: id WARNING: Can't mass-assign these protected attributes: id WARNING: Can't mass-assign these protected attributes: id

If i use only

> attr_accessible :id, :rohstoffhauptgruppenname << the error raised is: WARNING: Can't mass-assign these protected attributes: untergruppen_zeigen

if a assign untergruppen_zeigen as well to attr_accessible

above error message show up.

I have no clou how to resolve that problem. help is welcome!

Thanks TimE