Rails 5嵌套属性“未允许的参数” - 白名单

时间:2016-09-15 14:12:26

标签: ruby-on-rails rails-activerecord ruby-on-rails-5

错误:未允许的参数:属性

我将request_controller.rb中的属性{}列入白名单 这通常有效,但不是这次。

我无法保存表单中输入的部分数据。未保存的3个字段来自动态表单“request_type”。我按照Rails Cast第403集进行了这个解决方案,我在另一个项目中工作得很好但不在这个项目中。

来源:http://railscasts.com/episodes/403-dynamic-forms

很抱歉,如果这是一个重复的问题,但我已经看了几个其他问题,我不能指出我在这里做错了什么

我已经在这里研究了几个问题,但我仍然无法让它发挥作用:
Rails 4 Nested Attributes Unpermitted Parameters
Nested attributes - Unpermitted parameters Rails 4

我省略了一些东西,以便更容易阅读代码。请问我是否需要了解更多信息。

以下是日志:

Processing by RequestsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8EASewOIxY58b+SU+dxd2YAfpjt38IdwNSju69RPwl/OKfx3AfmvLav79igj8CqPbDwi0eJAwojRbtm+C9F6wg==", "request"=>{"name"=>"asdasddaa", "due_date(1i)"=>"2016", "due_date(2i)"=>"9", "due_date(3i)"=>"15", "user_id"=>"1", "project_id"=>"1", "request_type_id"=>"2", "properties"=>{"Name and last name"=>"asdasd", "Mobile"=>"asdada", "Office tel."=>"asdadas"}}, "commit"=>"Create Request"}
Unpermitted parameter: properties

更新

如果我将request_params更改为:

def request_params
  params.require(:request).permit(:name, :due_date, :group_id, :user_id, :project_id, :request_type_id, properties:{} ).tap do |whitelisted|
    whitelisted[:properties] = params[:request][:properties]
  end
end

查看: 属性:{}

我收到此错误:

Unpermitted parameters: Name and last name, Mobile, Office tel.

request_controller.rb

  def new
    @request = Request.new
    @request_type = RequestType.find(params[:request_type_id])
    @project = @request_type.project.id
  end

  def create
    @request = Request.new(request_params)

    respond_to do |format|
      if @request.save
        format.html { redirect_to @request, notice: 'Request was successfully created.' }
        format.json { render :show, status: :created, location: @request }
      else
        format.html { render :new }
        format.json { render json: @request.errors, status: :unprocessable_entity }
      end
    end
  end

    def request_params
      params.require(:request).permit(:name, :due_date, :group_id, :user_id, :project_id, :request_type_id, :properties).tap do |whitelisted|
        whitelisted[:properties] = params[:request][:properties]

      end
    end

request_types_controller.rb

  def new
    @request_type = RequestType.new
    @project = Project.find(params[:project])
  end

  def create
    @request_type = RequestType.new(request_type_params)

    respond_to do |format|
      if @request_type.save
        format.html { redirect_to @request_type, notice: 'Request type was successfully created.' }
        format.json { render :show, status: :created, location: @request_type }
      else
        format.html { render :new }
        format.json { render json: @request_type.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @request_type.update(request_type_params)
        format.html { redirect_to @request_type, notice: 'Request type was successfully updated.' }
        format.json { render :show, status: :ok, location: @request_type }
      else
        format.html { render :edit }
        format.json { render json: @request_type.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_request_type
      @request_type = RequestType.find(params[:id])
    end


    def request_type_params
      params.require(:request_type).permit(:name, :project_id, properties:{}, fields_attributes: [:name, :field_type, :required, :id, :_destroy])
      # params.require(:product_type).permit(:name, :product_type_id)      
    end

模型/ request.rb

class Request < ApplicationRecord
  belongs_to :group
  belongs_to :user
  belongs_to :project
  belongs_to :request_type
  serialize :properties, Hash
end

模型/ request_type.rb

class RequestType < ApplicationRecord
  belongs_to :project
  has_many :fields, class_name: "RequestField"
  accepts_nested_attributes_for :fields, allow_destroy: true
  has_many :requests

end

模型/ request_field.rb

class RequestField < ApplicationRecord
  belongs_to :request_type
end

视图/请求/ new.html.erb

<%= form_for @request do |f| %>

  <%= f.fields_for :properties, OpenStruct.new(@request.properties) do |builder| %>
    <% @request_type.fields.each do |field| %>
      <%= render "requests/fields/#{field.field_type}", field: field, f: builder %>
    <% end %>
  <% end %>            


  <div class="actions">
    <%= f.submit class:"btn btn-primary" %>
  </div>
<% end %>

1 个答案:

答案 0 :(得分:0)

尝试从request_controller中的request_params中删除:属性,如下所示:

def request_params
  params.require(:request).permit(:name, :due_date, :group_id, :user_id, :project_id, :request_type_id).tap do |whitelisted|
    whitelisted[:properties] = params[:request][:properties]
end

修改

def request_params
  params.require(:request).permit(:id, :name, :due_date, :group_id, :user_id, :project_id, :request_type_id)
  params.require(:properties).permit!
end!