gpt4 book ai didi

ruby-on-rails - 强参数 : Hash param is missing or the value is empty

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:53 24 4
gpt4 key购买 nike

我有一个表单,其中有一个带有复选框的表格,以便选择行并将 ID 保存在我的数据库中。

但它抛出一个错误:

Param is missing or the value is empty: 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"}

Controller

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

路线.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 并使用this documentation查看可用选项。

这会将您的属性嵌套在 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"}

关于ruby-on-rails - 强参数 : Hash param is missing or the value is empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33025937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com