Rails 4嵌套属性具有未允许的参数错误

时间:2015-06-09 09:21:25

标签: ruby-on-rails

以下是我的api模型:

class Api < ActiveRecord::Base
  validates_presence_of :name
  belongs_to :service
  has_many :statuses
  accepts_nested_attributes_for :statuses, reject_if: proc { |attributes| attributes['name'].blank?  }
end

以下是我的描述模型:

class Description < ActiveRecord::Base
  validates_presence_of :value
  belongs_to :status
end

以下是我的状态模型:

class Status < ActiveRecord::Base
  belongs_to :api
  has_many :descriptions
  accepts_nested_attributes_for :descriptions, reject_if: proc { |attributes| attributes['value'].blank?  }
end

以下是我的新手动控制器:

  def new
    @api = Api.new
    @status = @api.statuses.new
    @status.descriptions.new
  end

  def create
    @api = Api.new(api_params)
    if @api.save
    flash[:info] = request.original_url + ".do?apiname=" + "#{@api.name}"
    redirect_to root_path
  else 
    @api.statuses.new
    render :new
  end


  private

  def api_params
    params.require(:api).permit(:name, statuses_attributes: [:name, descriptions_attributes:[:value]])
  end

以下是我的新模板:

<div class="form-horizontal">
<%= form_for @api, :url => commons_path do |f| %>
  <div class="form-group">
    <%= f.label :name, "API Name", class: "col-sm-2 control-label" %>
    <div class="col-sm-8">
      <%= f.text_field :name, class: "form-control" %>
    </div>       
  </div>

  <%= f.fields_for :statuses do |status| %>
    <div class="form-group">
      <%= status.label :name, "Status",  class: "col-sm-2 control-label" %>
      <div class="col-sm-8">
        <%= status.text_field :name, class: "form-control" %>
      </div>
    </div>

    <%= f.fields_for :description do |description| %>
      <div class="form-group">
        <%= description.label :value, "Body",  class: "col-sm-2 control-label" %>
        <div class="col-sm-8">
          <%= description.text_area :value, class: "form-control", rows: 12, cols: 65 %>
        </div>
      </div>
    <% end %>
  <% end %>

    <%= f.submit("Create Data", class: 'btn btn-primary col-sm-offset-2') %>
    <%= link_to "Cancel", root_path, class: "btn btn-danger" %>  
<% end %>

我创建新数据后。似乎只有描述的值没有保存到我的数据库中。并在我的控制台中找到错误“未经许可的参数:说明”。

任何人都知道会发生什么?

1 个答案:

答案 0 :(得分:1)

<div class="form-horizontal">
<%= form_for @api, :url => commons_path do |f| %>
  <div class="form-group">
    <%= f.label :name, "API Name", class: "col-sm-2 control-label" %>
    <div class="col-sm-8">
      <%= f.text_field :name, class: "form-control" %>
    </div>       
  </div>

  <%= f.fields_for :statuses do |status| %>
    <div class="form-group">
      <%= status.label :name, "Status",  class: "col-sm-2 control-label" %>
      <div class="col-sm-8">
        <%= status.text_field :name, class: "form-control" %>
      </div>
    </div>

    <%= status.fields_for :descriptions do |description| %>
      <div class="form-group">
        <%= description.label :value, "Body",  class: "col-sm-2 control-label" %>
        <div class="col-sm-8">
          <%= description.text_area :value, class: "form-control", rows: 12, cols: 65 %>
        </div>
      </div>
    <% end %>
  <% end %>

    <%= f.submit("Create Data", class: 'btn btn-primary col-sm-offset-2') %>
    <%= link_to "Cancel", root_path, class: "btn btn-danger" %>  
<% end %>