0

我无法找到为什么我的强参数没有保存在数据库中的原因。我没有使用脚手架来创建模型,而是使用rails g model Customer Name:string Email:string命令。使用rails console.

这是我的页面控制器

class PagesController < ApplicationController 

def Home

end

def Login

@customer=Customer.new(master)
@customer.save

end
 private
 def attr @customer=Customer.find(params[:id]) end 
def master params.require(:customer).permit(:Name,:Email) end end

我的 customer.rb 文件

class Customer < ActiveRecord::Base

    has_many :Products,through: :Order

end

尝试保存新客户时的开发日志。

Started GET "/Login?Customer%5BName%5D=s&Customer%5BEmail%5D=csc" for 127.0.0.1 at 2013-10-31 04:48:49 +0530 Processing by PagesController#Login as HTML Parameters: 

{"Customer"=>{"Name"=>"s", "Email"=>"csc"}} (0.1ms) begin transaction SQL (0.5ms)

 INSERT INTO "customers" ("created_at", "updated_at") VALUES (?, ?) [["created_at", Wed, 30 Oct 2013 23:18:49 UTC +00:00], ["updated_at", Wed, 30 Oct 2013 23:18:49 UTC +00:00]] (72.4ms) commit transaction Rendered pages/Login.html.erb within layouts/application (0.1ms) Completed 200 OK in 82ms (Views: 5.7ms | ActiveRecord: 73.0ms)

在浏览器中打开页面时出现的错误:

ActionController::ParameterMissing in PagesController#Login
param not found: customer
Extracted source (around line #17):
4

2 回答 2

0

案件很重要。你的传递Customer,但要求customer。这些是不同的参数。将所有内容切换为小写,除了实际的类。

于 2013-10-30T18:41:03.947 回答
0

params.require你指定customer的参数中,你有Customer。这会有所不同。根据经验:始终使用小写字母。

于 2013-10-30T18:41:18.947 回答