簡體   English   中英

強大參數:哈希參數缺失或值為空

[英]Strong Parameters : Hash param is missing or the value is empty

我有一個表格,其中有一個帶有復選框的表格,以便選擇行並將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

型號

class Leadallocation < ActiveRecord::Base
  belongs_to :campaign
  belongs_to :company
  belongs_to :user
end

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  has_many :activities
  belongs_to :campaign
  has_many :leadallocations

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

class Company < ActiveRecord::Base
  belongs_to :campaign
  has_many :subsidiaries
  has_many :leadallocations
end

class Campaign < ActiveRecord::Base
  belongs_to :client
  has_many :leads
  has_many :activities
  has_many :contacts
  has_many :users
has_many :leadallocations

end

的routes.rb

resources :leadallocations

風景

    <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

問題出在您的表單中,而不是check_box_tag ,您需要執行f.check_box並使用此文檔查看可用選項。

這會將您的屬性嵌套在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"]

應該是:

"leadallocation" => {
    "company_ids" => ["38", "40"]
}

這是將Rails對象傳遞到數據庫的標准構造。 這就是為什么strong_params是以這種方式進行結構化的原因...

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

-

您只需使用.permit 即可解決此.permit

#app/controllers/leadallocations_controller.rb
class LeadAllocationsController < ApplicationController
    private
    def leadallocation_params
       params.permit(:company_ids)
     end
end

這不是永久性修復,而是HACK !!!

固定:

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

這里參考:

應該送你需要返回到您的控制器的數據。 注意f.___這告訴Rails使用您在@leadallocations周圍調用的FormBuilder來構建表單。

簡而言之,這意味着您的數據將封裝在{"leadallocations" => ...}參數中,而如果您僅使用check_box_tag ,它將假定數據是獨立的。

另外, 切勿使用HTML來樣式化頁面。 <br><table>的使用用於格式化。 CSS是樣式化應用程序的唯一方法。 您使用<table> 似乎還可以 我只想強調一下,因為大多數人不了解HTML和CSS。


此外,還有另一個問題-與您的關聯:

class Leadallocation < ActiveRecord::Base
  belongs_to :company
end

您不能將多個公司ID分配給一個belongs_to關系:

在此處輸入圖片說明

我見過可以傳遞[foreign_key]_ids實例,但是不建議這樣做; 確實是反模式

您將要尋找的是has_and_belongs_to_many

在此處輸入圖片說明

#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

這肯定會允許您關聯每個公司的多個leadallocations ,反之亦然。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM