2
這是我第一次搞嵌套屬性。我遇到了一個問題,我試圖創建一個具有「令牌」及其屬性的「學校」。提交表單後,即使我使用令牌屬性的令牌值提交表單,我也會收到錯誤,說明「令牌屬性不能爲空」(我的模型驗證的b/c)。Rails 4嵌套屬性和強參數
我認爲在查看服務器日誌時,事情並未對齊,但我不知道爲什麼?
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xbDhfXJotAfgg6O9rnrSuKy01cxoTi/ZpgaDuD4fkQA=", "school"=>{"name"=>"Palmer", "address"=>"123 Palmer lane", "city"=>"Baldwinsville", "state"=>"CA", "zip"=>"10001", "tokens_attributes"=>{"0"=>{"database"=>"Rhetorical studies", "start_date(1i)"=>"2013", "start_date(2i)"=>"8", "start_date(3i)"=>"29", "expiration_date(1i)"=>"2014", "expiration_date(2i)"=>"8", "expiration_date(3i)"=>"29"}}}, "commit"=>"Update"}
我的學校模式是這樣的:
class School < ActiveRecord::Base
has_many :users
has_many :tokens
accepts_nested_attributes_for :tokens
end
的schools_controller的新動作如下所示:
def new
@school = School.new
@school.tokens.build
end
我的學校的形式有以下fields_for:
<%= f.fields_for :tokens do |builder| %>
<p>
<%= builder.label "Database(s)" %>
<%= builder.text_field :database %>
<p>
<p>
<%= builder.label "Start Date" %><br />
<%= builder.date_select :start_date %>
<p>
<p>
<%= builder.label "Expiration Date" %><br />
<%= builder.date_select :expiration_date %>
<p>
<hr />
<% end -%>
And th E「school_params」強於「schools_controller.rb」 PARAMS是這樣的:
def school_params
params.require(:school).permit(:name, :address, :city, :state, :zip, tokens_attributes: [:id, :user_id, :school_id, :database, :start_date, :expiration_date])
end
基於日誌,它好像我做錯了什麼在「school_params」。有什麼想法嗎?