hello i'm trying post nested attributes backend. posting new model works fine long don't try include nested model.
my form.html.erb:
<%= simple_form_for(@petition, :url => petitions_path, :html => {:multipart => true}, :html => {:class => 'form-horizontal' }) |f| %> <%= f.simple_fields_for :userdetail_attributes |m| %> <%= m.input :anrede, :label => "anrede", collection: ["frau","herr","familie"], label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' } %> <%= m.input :titel, :label => "akadem. grad/bez.", label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' }%> <%= m.input :name, :label => "vorname", label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' } %> <%= m.input :last_name, :label => "nachname", label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' } %> <%= m.input :institution , :label => "institution", label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' }%> <%= m.input :zip, :label => "postleizahl", label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' } %> <%= m.input :city, :label => "stadt", label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' } %> <%= m.input :street, :label => "straße", label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' } %> <%= m.input :street_number, :label => "hausnummer", label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' } %> <%= m.input :state, :label => "staatsangehörigkeit", label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' } %> <%= m.input :telefon, :label => "telefon", label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' } %> <%= m.input :fax, :label => "fax", label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' } %> <%= m.input :email, :label => "e-mail", label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' } %> <% end %> <%= f.input :title, :label => "titel der petition", label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' }%> <%= f.input :ziel, :label => "welches ziel hat ihre petition?",:as => :text, label_html: {class: 'span4'}, input_html: { class: 'span11 pull-right' }%> ... <%= f.button :submit, "petition einreichen", :class => "blue btn pull-right" %> <% end %>
when hit button frontend sends_out following
started post "/petitions" 127.0.0.1 @ 2013-07-11 11:58:23 +0200 processing petitionscontroller#create html parameters: {"utf8"=>"✓", "authenticity_token"=>"u04pq55e7qy3nyoh2pwhm5tp6mawkrq9thk5ibgx3+c=", "api_petition"=>{ "userdetail_attributes"=>{ "anrede"=>"frau", "titel"=>"prof", "name"=>"xxxx", "last_name"=>"xxx", "institution"=>"123", "zip"=>"00000", "city"=>"stuttgart", "street"=>"klemmenstr", "street_number"=>"12", "state"=>"d", "telefon"=>"", "fax"=>"", "email"=>"example@mail.de"}, "title"=>"sem malesuada consectetur nibh mollis", "ziel"=>"aenean lacinia bibendum nulla sed consectetur. lorem ipsum dolor sit amet, consectetur adipiscing elit...", ... }, "commit"=>"petition einreichen"}
and dies...like that
set authtoken --> hdrwi8cxjijbntyzx5mt <-- , xxx http://localhost:4000/users/1.json --> 200 ok 10102 (283.0ms) post http://localhost:4000/petitions.json --> 500 internal server error 8448 (102.8ms) completed 500 internal server error in 391ms
my backend receives fancy bit
started post "/petitions.json" 127.0.0.1 @ 2013-07-11 11:42:53 +0200 processing petitionscontroller#create json parameters: {"petition"=>{"akzeptiert_agb"=>"1", "gegen_was"=>"", "gegen_wen"=>"", "gesetzes_aenderung"=>"", "oeffentlich"=>"1", "pstatus_id"=>1, "rechtsbehelfe"=>"", "title"=>"sem malesuada consectetur nibh mollis", "umstand"=>"", "user_id"=>1, "userdetail_attributes"=>{ "userdetail_attributes"=>{ "anrede"=>"frau", "city"=>"weimar", "email"=>"example@mail.de", "fax"=>"", "institution"=>"123", "last_name"=>"name", "name"=>"name", "state"=>"d", "street"=>"klemmenstr", "street_number"=>"12", "telefon"=>"", "titel"=>"prof", "zip"=>"99423" } }, "ziel"=>"aenean lacinia bibendum nulla sed consectetur. lorem ipsum dolor sit amet, consectetur adipiscing elit..." }} completed 500 internal server error in 2ms
i have idea duplication of userdetail attributes comes from. gues leads following error in backend:
activemodel::massassignmentsecurity::error - can't mass-assign protected attributes: userdetail_attributes:
my petitions_controller.rb in frontend:
def new @petition = api::petition.new @userdetail = userdetail.new respond_to |format| format.html # new.html.erb format.json { render json: @petition } end end # post /petitions # post /petitions.json def create @petition = api::petition.new(params[:api_petition]) @petition.user_id = current_user.id @petition.pstatus_id = 1 if !params[:userdetail].nil? @petition.userdetail = userdetail.new(params[:userdetail]) @petition.userdetail.user_id = current_user.id end respond_to |format| if @petition.save format.html { redirect_to user_path(current_user), notice: 'ihre petition wurde erfolgreich zur weiteren prüfung überstellt.' } else format.html { render action: "new" , notice: 'bedauerlicherweise ist etwas schiefgelaufen, ihre petition wurde nicht gespeichert'} end end end
my petition.rb in backend:
class petition < activerecord::base has_one :userdetail accepts_nested_attributes_for :userdetail #, :reject_if => proc { |attributes| attributes['userdetail'].blank? } attr_accessible :title, :akzeptiert_agb, :begruendung, :gegen_was, :gegen_wen, :gesetzes_aenderung, :oeffentlich, :rechtsbehelfe, :umstand, :ziel, :pstatus, :user, :voters, :user_id, :pdf, :can_vote, :count_users_voted, :phase, :started, :neu, :beratung, :archiv, :private_pdf, :abschlussberichts, :abschlussberichts, :status, :moderated, :moderationdate, :userdetail
my petitions_controller.rb in backend
# /petitions/new # /petitions/new.json def new @petition = petition.new @userdetail = @petition.userdetail.new respond_to |format| format.html # new.html.erb format.json { render json: @petition.to_json(:include => [@userdetail]) } end end # post /petitions # post /petitions.json def create @petition = petition.new(params[:petition]) #@petition.user = user.find(current_user.id) || user.find_by_authentication_token(session[:auth_token]) @petition.pstatus = pstatus.find(1) if params[:userdetail].nil? @petition.userdetail = userdetail.new(params[:userdetail]) end respond_to |format| if @petition.save format.html { redirect_to user_path(current_user || user.find_by_authentication_token(session[:auth_token]).id ), notice: 'ihre petition wurde erfolgreich zur weiteren prüfung überstellt.' } format.json { render json: @petition, status: :created, location: @petition } else format.html { render action: "new" , notice: 'irgendetwas ist gerade schiefgelaufen. bitte wenden sie sich an: 03643/778525.'} format.json { render json: @petition.errors, status: :unprocessable_entity } end end end
my userdetails.rb in backend
class userdetail < activerecord::base belongs_to :user belongs_to :petition attr_accessible :anrede, :city, :email, :fax, :institution, :last_name, :name, :state, :street, :street_number, :telefon, :titel, :zip
has idea how fix this? i've been trying lot still can't head around nesting of userdetails_attributes. in advance , sorry ultra long text.
just add
:userdetail_attributes
to
attr_accessible
or :userdetails
, idea should plural.
Comments
Post a Comment