您好我正在尝试将嵌套属性发布到我的后端。只要我不尝试包含嵌套模型,发布新模型就可以正常工作。
my 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ß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 %>
当我按下按钮时,我的前端发送以下
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"=>"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"}
并且死了......就像那样
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"=>"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
我现在知道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
后端的userdetails.rb
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_attributes的嵌套。在此先感谢并为超长文本感到抱歉。
答案 0 :(得分:0)
添加
:userdetail_attributes
到
attr_accessible
或:userdetails
,根据想法应为复数。
答案 1 :(得分:0)
以下部分应该看起来不同,
"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"
}
},
像这样,
"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"
}