0

您好,我试图将嵌套属性发布到我的后端。只要我不试图包含我的嵌套模型,发布新模型就可以正常工作。从活动资源前端发布嵌套属性到活动模型后端

我form.html.erb:

<%= simple_form_for(@petition, :url => petitions_path, :html => {:multipart => true}, :html => {:class => 'form-horizontal' }) do |f| %> 
    <%= f.simple_fields_for :userdetail_attributes do |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&szlig;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 %> 

当我按下按钮我的前端sends_out以下

Started POST "/petitions" for 127.0.0.1 at 2013-07-11 11:58:23 +0200 
Processing by PetitionsController#create as 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"=>"[email protected]"}, 
    "title"=>"Sem Malesuada Consectetur Nibh Mollis", 
    "ziel"=>"Aenean lacinia bibendum nulla sed consectetur. Lorem ipsum dolor sit amet, consectetur adipiscing elit...", 
     ... }, "commit"=>"Petition einreichen"} 

和死亡......这样

set authtoken --> Hdrwi8cxjijBNtyZX5MT <-- , xxx 
GET 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 

我后端比接收这个花式位

Started POST "/petitions.json" for 127.0.0.1 at 2013-07-11 11:42:53 +0200 
Processing by PetitionsController#create as 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"=>"[email protected]", 
     "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 

我现在已经想到了userdetail属性的重复来自何处。 但我客串是什么导致了下面的错误在我的后端:

ActiveModel::MassAssignmentSecurity::Error - Can't mass-assign protected attributes: userdetail_attributes: 

我petitions_controller.rb在前端:

def new 
    @petition = Api::Petition.new 
    @userdetail = Userdetail.new 
    respond_to do |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 do |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 

我petition.rb在后端:

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 

我在后端的petitions_controller.rb

 # GET /petitions/new 
     # GET /petitions/new.json 
     def new 
     @petition = Petition.new 
     @userdetail = @petition.userdetail.new 
     respond_to do |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 do |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 

我在后台

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 

userdetails.rb有没有人一个想法如何解决这一问题?我一直在尝试很多,但仍然无法绕过userdetails_attributes的嵌套。提前致谢,并对超长文本感到抱歉。

回答

0

只需添加

:userdetail_attributes 

attr_accessible 

:userdetails,通过思路应该是复数。

+0

感谢,但不解决问题。之前已经尝试过,但没有效果。 – bbuegler

+0

嗯...在我的应用程序中工作,3.2 –

0

这接下来的部分应该是不同的,

"userdetail_attributes"=>{ 
    "userdetail_attributes"=>{ 
     "anrede"=>"Frau", "city"=>"Weimar", "email"=>"[email protected]", 
     "fax"=>"", "institution"=>"123", "last_name"=>"name", "name"=>"name", 
     "state"=>"d", "street"=>"klemmenstr", "street_number"=>"12", "telefon"=>"", 
     "titel"=>"prof", "zip"=>"99423" 
    } 
    }, 

这样,

"userdetail_attributes"=>{ 
    "anrede"=>"Frau", "city"=>"Weimar", "email"=>"[email protected]", 
    "fax"=>"", "institution"=>"123", "last_name"=>"name", "name"=>"name", 
    "state"=>"d", "street"=>"klemmenstr", "street_number"=>"12", "telefon"=>"", 
    "titel"=>"prof", "zip"=>"99423" 
} 
+0

这也是我的想法,但它来自哪里?任何想法? – bbuegler

+0

也许改变这一点,<%= f.simple_fields_for:userdetail_attributes do | m | %>到<%= form_tag%>以及其中的所有内容来输入标记。 – beck03076