2

这是我第一次弄乱嵌套属性。我在尝试创建具有“令牌​​”及其属性的“学校”时遇到问题。提交表单后,即使我提交的表单带有 Token 属性的 Token 值,我也会收到错误消息“令牌属性不能为空”(我的模型验证的 b/c)。

我认为在查看服务器日志时事情有误,但我不确定为什么?

  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

school_controller 的新操作如下所示:

  def new
    @school = School.new
    @school.tokens.build
  end

我的学校表格包含以下字段:

<%= 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 -%>

'schools_controller.rb' 中的 'school_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”中做错了什么。有什么想法吗?

4

1 回答 1

0

Oops. I was working on this late last night. In doing so I had '@school.tokens.build' in both the 'new' and the 'create' actions. Removed it from 'create', and it's working fine now.

于 2013-08-29T18:58:11.227 回答