[英]Rails 5 Unpermitted parameter
我正在嘗試做一件簡單的事情,但是我無法通過!
簡單的筆記本應用程序:聯系人,電話和電話類型。
楷模:
class Contact < ApplicationRecord
has_many :phones, inverse_of: :contact
accepts_nested_attributes_for :phones, allow_destroy: true
end
class NumberType < ApplicationRecord
has_many :phones, inverse_of: :number_types
end
class Phone < ApplicationRecord
belongs_to :number_type
belongs_to :contact
end
控制器:
def contact_params
params.require(:contact).permit(:name,
phone_attributes:[:number, :number_type_id, :contact_id, :_destroy])
end
視圖:
<%= form_with(model: contact, local: true) do |form| %>
.
.
.
<div class="field">
<%= form.label :name %>
<%= form.text_field :name, id: :contact_name %>
</div>
<%=form.fields_for :phone do |phone_field| %>
<div class="field">
<%= phone_field.label :number %>
<%= phone_field.text_field :number %>
</div>
<div class="field">
<%= phone_field.label :number_type %>
<%= collection_select(:phone, :number_type_id, @number_types_for_select, :id, :description) %>
</div>
<%end%>
事情是,我可以保存一個新聯系人,但它不保存電話號碼...日志中顯示“ Unpermitted parameter:”(保存的參數:),它保存了聯系人姓名...但沒有保存其余的...
我也已經為測試創建了一些PhoneType。
這是日志:
Started POST "/contacts" for 127.0.0.1 at 2017-07-21 17:03:48 -0300
Processing by ContactsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"n4LQ6EjssSBp6yCTOni1B9qKUAygzUJMMTN+12Fth6AO2dblXmySdOdjab/ABg3PI19oF9nsyGIVjYa3YyAVPw==", "contact"=>{"name"=>"Mendel", "phone"=>{"number"=>"1234567890"}}, "phone"=>{"number_type_id"=>"1"}, "commit"=>"Create Contact"}
Unpermitted parameter: :phone
(0.1ms) begin transaction
SQL (0.8ms) INSERT INTO "contacts" ("name", "created_at", "updated_at") VALUES (?, ?, ?) [["name", "Mendel"], ["created_at", "2017-07-21 20:03:48.952923"], ["updated_at", "2017-07-21 20:03:48.952923"]]
(0.6ms) commit transaction
所以! 我不知道在這里要更改什么。 我一直在這里閱讀答案,但我做對了。 我對此非常了解,並且對編碼也很自省。
幫助將不勝感激! :)
謝謝
<-! 第2部分--->
因此,這就是問題所在:
創建新聯系人時,我得到:
電話號碼類型必須存在。
視圖如下:
<div class = "nested-fields">
<div class="field">
<%= f.label :number %>
<%= f.text_field :number %>
<%= f.label :number_type %>
<%= collection_select(:phone, :number_type_id, NumberType.all, :id, :description) %>
<%= link_to_remove_association('[delete]',f)%>
</div>
</div>
作為部分調用:
<%=form.fields_for :phones do |phone_field| %>
<%= render partial: "phone_fields", locals: {f: phone_field} %>
<%end%>
<%=link_to_add_association('[+ phone]', form, :phones)%>
是在Params上獲得許可還是在設置集合選擇時設置了問題?
這是我在服務器上得到的:
Started POST "/contacts" for 127.0.0.1 at 2017-07-22 13:13:24 -0300
Processing by ContactsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PJxUoaoaAQh5Q7sXKgkUrnfppNQjRTLhdkyaIJdifDKtx1KsvJoiXPfL8jvQd6xmjjycz1pkuM9S8mJAlS/urQ==", "contact"=>{"name"=>"Mendel", "phones_attributes"=>{"1500740000317"=>{"number"=>"123456", "_destroy"=>"false"}}}, "phone"=>{"number_type_id"=>"1"}, "commit"=>"Create Contact"}
(0.1ms) begin transaction
(0.1ms) rollback transaction
再次感謝你!
仔細注意導軌中的多元化。 如果您的關聯是has_many
或has_and_belongs_to_many
,則需要始終使用復數形式。
def contact_params
params.require(:contact).permit(:name,
phones_attributes:[:number, :number_type_id, :contact_id, :_destroy])
end
<%= form_with(model: contact, local: true) do |form| %>
<div class="field">
<%= form.label :name %>
<%= form.text_field :name, id: :contact_name %>
</div>
<%= form.fields_for :phones do |phone_field| %>
<div class="field">
<%= phone_field.label :number %>
<%= phone_field.text_field :number %>
</div>
<div class="field">
<%= phone_field.label :number_type_id %>
<%= collection_select(:phone, :number_type_id, @number_types_for_select, :id, :description) %>
</div>
<% end %>
<% end %>
好的,所以我不知道為什么,但是我知道了。
問題出在collection_select代碼上。
它是 :
<%= collection_select(:phone, :number_type_id, NumberType.all, :id, :description) %>
它的工作原理是:
<%= f.collection_select(:number_type_id, NumberType.all, :id, :description) %>
如果知道為什么,我很想了解!
謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.