gpt4 book ai didi

ruby-on-rails - 创建中的 Rails 4 错误 "param is missing or the value is empty:"

转载 作者:数据小太阳 更新时间:2023-10-29 07:44:57 24 4
gpt4 key购买 nike

我看过几个教程、ruby 指南和几个 stackoverflow 问题。我先尝试使用 simple_form,现在尝试使用老式方法,无法弄清楚为什么没有传递参数。

Controller :

def new
@topgem = Topgem.new
end

def create
@topgem = Topgem.new(topgem_params)

if @topgem.save
redirect_to @topgem
else
render 'new'
end

...

 private
def topgem_params
params.require(:name).permit(:url, :description, :downloads, :last_updated)
end

型号:

class Topgem < ActiveRecord::Base

has_many :votes
has_many :users, through: :votes

validates :name, presence: true, uniqueness: true, :length => {
:minimum =>2,
:maximum =>50}

validates :url, presence: true
validates :description, presence: true
validates :downloads, numericality: { only_integer: true }
end

new.html.erb

<%= form_for(@topgem) do |f| %>


<%= f.label :name %>:
<%= f.text_field :name %><br />

<%= f.label :url %>:
<%= f.text_field :url %><br />

<%= f.label :description %>:
<%= f.text_field :description %><br />

<%= f.label :downloads %>:
<%= f.number_field :downloads %><br />


<%= f.submit %>
<% end %>

我得到的错误:

ActionController::ParameterMissing at /topgems
param is missing or the value is empty: name

这里是选择的实例变量:

实例变量

@_action_has_layout 
true

@_routes
nil

@_headers
{"Content-Type"=>"text/html"}

@_status
200

@_params
{"utf8"=>"✓", "authenticity_token"=>"Gx/UwvcvWZYWAUHxWGYlUQB/PNNUniBpCjlM1WEHAm+luYl94Kky5Ae9Ur40YVtrN2ebEEX8C0G3Cewu/SJSow==", "topgem"=>{"name"=>"bfgf", "url"=>"dd", "description"=>"ff", "downloads"=>"343"}, "commit"=>"Create Topgem", "controller"=>"topgems", "action"=>"create"}

最佳答案

您需要 params[:name],但实际参数是 params[:topgem][:name]

将您的 topgem_params 方法更改为

params.require(:topgem).
permit(
:name,
:url,
:description,
:downloads,
:last_updated
)

关于ruby-on-rails - 创建中的 Rails 4 错误 "param is missing or the value is empty:",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28664074/

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