参数缺失或值为空:center

param is missing or the value is empty: center

我是新手 rails 我看到这个问题经常被问到,但我仍然遇到一些问题。谁能告诉我我哪里做错了。
[center_controller]

class CentersController < ApplicationController

  def index
    @centers=Center.all
  end 

  def new
  @center=Center.new
  end

  def create
    @center=Center.new(para)
    if @center.save
        flash[:notice] = "success man."
            redirect_to @center
    else
        render 'new'
    end
  end

  private
    def para
     params.require(:center).permit(:registration_number,:registration_date,
:tin_number,:no_of_trainers,:total_numbers,:name,:address,
:contact_no,:registration_fee,:monthly_fee,:status,:sector_id)
    end

end


[center/index.html.erb]

<%=form_for :centers_index_path do |f| %>
            <fieldset>
                <legend>Centers</legend>
                <div>
                    <%= f.text_field :registration_number, :placeholder=>"Enter registration number" %>
                </div>
                  <div>
                    <%= f.text_field :registration_date, :placeholder=>"Enter registration date" %>
                </div>
<%= f.submit :submit %>    
            </fieldset>    
        <% end %>
  <br>

[center.rb]

class Center < ActiveRecord::Base
validates :registration_number,presence: true
validates :registration_date
end


[移民]

class CreateCenters < ActiveRecord::Migration
  def up
    create_table :centers do |t|
        t.integer :registration_number
        t.date :registration_date
        t.integer :tin_number
        t.integer :no_of_trainers
        t.integer :total_numbers
        t.string :name
        t.string :address
        t.string :contact_no
        t.integer :registration_fee
        t.integer :monthly_fee
        t.integer :status
        t.integer :sector_id
        t.timestamps
    end
    add_foreign_key :centers,:sectors
  end


在控制台上收到此错误

 Started POST "/centers" for 127.0.0.1 at 2016-01-20 15:00:47 +0530
    Processing by CentersController#create as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"11rrttKOGdWrCClqnB/aTuHWLT4UsoQRWkpiwwk6bBxgSBO85f532LytX3d6rJml8cW3lMjnvB6eUpAeTHBA6A==", "centers_index_path"=>{"text"=>"", "password"=>"[FILTERED]"}, "commit"=>"submit"}
    Completed 400 Bad Request in 2ms (ActiveRecord: 0.0ms)

您应该在 form_for!

中使用 @center
<%=form_for @center do |f| %>

这会将表单绑定到您的模型,并且参数实际上会到达他们需要的地方!

new 操作 (GET) 上,它将创建一个新的空 center,在 create 操作 (POST) 上,它将填充 center用表格的数据!

应该可以!

更新:

您没有使用 new 操作,您应该将表单作为部分文件放在名为 new.html.erb 的文件中,然后调用 new。

我建议您生成一个新的脚手架 以查看 REST 设置应该是什么样子并从那里开始。你可以这样做:

rails g scaffold my_center registration_number:integer registration_date:date tin_number:integer ... ...

然后:

rake db:migrate

这将为您提供 my_center 的工作示例,并且非常容易调整!

请检查您的form_for 如果您需要传递表单值来创建操作,那么以下代码就足够了。

<%= form_for @center do |f| %>
<% end  %>

如果您想将表单值传递给另一个操作,则需要为其设置 url 路径。

<%= form_for @center, url: center_save_path do |f| %>
<% end %>