Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 强参数:缺少哈希参数或值为空_Ruby On Rails_Ruby_Parameters - Fatal编程技术网

Ruby on rails 强参数:缺少哈希参数或值为空

Ruby on rails 强参数:缺少哈希参数或值为空,ruby-on-rails,ruby,parameters,Ruby On Rails,Ruby,Parameters,我有一个表单,其中有一个带有复选框的表,用于选择行并在数据库中保存ID 但它给了我一个错误: 缺少参数或值为空:leadallocation“ 我尝试了不同的方法,但仍然不起作用。所以现在我只是想把散列保存在我的数据库中。谢谢 # Never trust parameters from the scary internet, only allow the white list through. def leadallocation_params params.require(:leadall

我有一个表单,其中有一个带有复选框的表,用于选择行并在数据库中保存ID

但它给了我一个错误:

缺少参数或值为空:leadallocation“

我尝试了不同的方法,但仍然不起作用。所以现在我只是想把散列保存在我的数据库中。谢谢

# Never trust parameters from the scary internet, only allow the white list through.
def leadallocation_params
  params.require(:leadallocation).permit(:campaign_id, :company_id, :user_id)
end
请求参数:

{"utf8"=>"✓",
 "authenticity_token"=>"GruGL758jT4FO+t/BTRGLrD2uCGOj/qUCrB5VswquzR9N7JZ/rouLmGZnTE7A+XTARiLwkOy1n3/zMqhzuenmg==",
 "company_ids"=>["38",
 "40"],
 "commit"=>"Create Leadallocation"}
控制器

class LeadallocationsController < ApplicationController
  before_action :set_leadallocation, only: [:show, :edit, :update, :destroy]

  # GET /leadallocations
  # GET /leadallocations.json
  def complete
  end


  def index
    @leadallocations = Leadallocation.all
  end

  # GET /leadallocations/1
  # GET /leadallocations/1.json
  def show
  end

  # GET /leadallocations/new
  def new


    @leadallocation = Leadallocation.new
    @comps =  Company.all
  end

  # GET /leadallocations/1/edit
  def edit
  end

  # POST /leadallocations
  # POST /leadallocations.json
  def create

    @leadallocation = Leadallocation.new(leadallocation_params)

    @leadallocation.company_id = params[:company_ids]


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

  # PATCH/PUT /leadallocations/1
  # PATCH/PUT /leadallocations/1.json
  def update
    respond_to do |format|
      if @leadallocation.update(leadallocation_params)
        format.html { redirect_to @leadallocation, notice: 'Leadallocation was successfully updated.' }
        format.json { render :show, status: :ok, location: @leadallocation }
      else
        format.html { render :edit }
        format.json { render json: @leadallocation.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /leadallocations/1
  # DELETE /leadallocations/1.json
  def destroy
    @leadallocation.destroy
    respond_to do |format|
      format.html { redirect_to leadallocations_url, notice: 'Leadallocation was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def leadallocation_params
      params.require(:leadallocation).permit(:campaign_id, :company_id, :user_id)
    end
end
景色

    <h1>New Leadallocation</h1>

<p id="notice"><%= notice %></p>

<h1>Listing Companies</h1>
<%= simple_form_for(@leadallocation) do |f| %>

<table class="table table-bordered">
  <thead>
    <tr>
      <th></th>
      <th>Name</th> 
      <th>Country</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @comps.each do |company| %>
      <tr>
      <td><%= check_box_tag "company_ids[]", company.id %></td>
        <td><%= company.name %></td>
        <td><%= company.country %></td>

      </tr>
    <% end %>
  </tbody>
</table>

<br>
<div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>
<%= link_to 'Back', leadallocations_path %>
新的潜在客户分配

上市公司 名称 国家

使用强参数的方式要求参数遵循特定的形式

在这种情况下,您需要将参数嵌套在键
leadallocation

问题出在您的表单中,您需要执行
f,而不是
check\u-box\u-tag
。选中
f,然后使用查看可用选项

这将把您的属性嵌套在
leadallocation
下,如果没有嵌套,那么您的模型和表单设置可能有问题

基本上,所有这一切都是说,您希望更改以下参数:

 {"utf8"=>"✓",
  "authenticity_token"=>"GruGL758jT4FO+t/BTRGLrD2uCGOj/qUCrB5VswquzR9N7JZ/rouLmGZnTE7A+XTARiLwkOy1n3/zMqhzuenmg==",
  "company_ids"=>["38",
  "40"],
  "commit"=>"Create Leadallocation"}
为此:

 {"utf8"=>"✓",
  "authenticity_token"=>"GruGL758jT4FO+t/BTRGLrD2uCGOj/qUCrB5VswquzR9N7JZ/rouLmGZnTE7A+XTARiLwkOy1n3/zMqhzuenmg==",
  "leadallocation" => {
    "company_ids"=>["38","40"]
  }
  "commit"=>"Create Leadallocation"}
问题在于:

"company_ids"=>["38", "40"]
应该是:

这是将Rails对象传递给db的标准构造。这就是为什么它是这样构造的

params.require(:top_level_param).permit(:child_params)
--

您只需使用
。permit
,即可解决此问题:

#app/controllers/leadallocations_controller.rb
class LeadAllocationsController < ApplicationController
    private
    def leadallocation_params
       params.permit(:company_ids)
     end
end
不能为关系分配多个公司ID:

我见过可以传递
[foreign\u key]\u id的实例,但不建议这样做;事实上,一个

您需要的是:

#app/models/leadcallocation.rb
类LeadAllocation
这无疑将允许您关联每个公司的多个
leadallocations
,反之亦然

"leadallocation" => {
    "company_ids" => ["38", "40"]
}
params.require(:top_level_param).permit(:child_params)
#app/controllers/leadallocations_controller.rb
class LeadAllocationsController < ApplicationController
    private
    def leadallocation_params
       params.permit(:company_ids)
     end
end
#app/views/lead_allocations/new.html.erb
<%= simple_form_for @leadallocations do |f| %>
    <%= f.collection_check_boxes :company_ids, Company.all, :id, :name %>
    <%= f.submit %>
<% end %>
class Leadallocation < ActiveRecord::Base
  belongs_to :company
end
#app/models/leadallocation.rb
class LeadAllocation < ActiveRecord::Base
   has_and_belongs_to_many :company
end

#app/models/company.rb
class Company < ActiveRecord::Base
   has_and_belongs_to_many :leadallocations
end

#join table - companies_leadallocations