Rails 创建时出现 4 个错误 "param is missing or the value is empty:"

Rails 4 error "param is missing or the value is empty:" in create

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

控制器:

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

这里有 select 个实例变量:

实例变量

@_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
  )