我现在真的被困住了,而且很恼火。
我正在运行 Rails 2.3.5
我的 View/pages/customers.html.erb
简单地有:
<% form_tag do %>
First Name
<%= text_field_tag :firstName, params[:firstName] %>
Last Name
<%= text_field_tag :lastName, params[:lastName] %>
<%= submit_tag "Enter" %>
<%end%>
我的
Models/customer.rb
简单地有:class Customer < ActiveRecord::Base
attr_accessible :firstName, :lastName
end
我的
Controller/pages_controller
已class PagesController < ApplicationController
def custs
@cust = Customer.new(params[:firstName], params[:lastName])
@cust.save
end
end
如您所见,我只是想从前端输入两个字段,然后将它们保存到数据库中。但是,每当我加载页面时,它都会给我错误:
wrong number of arguments (2 for 1) pages_controller.rb:3:in
new' pages_controller.rb:3:in
custs'
奇怪的是,当我使用沙箱脚本/控制台时,我能够很好地插入数据。
这里发生了什么?请有人解释一下!
最佳答案
http://apidock.com/rails/ActiveRecord/Base/new/class这里是对新功能的一些解释。关键部分 - “传递具有与关联表列名匹配的键名的哈希”。而不是 @cust = Customer.new(params[:firstName], params[:lastName])
你应该有 @cust = Customer.new(:firstName => params[:firstName], :lastName => params[:lastName])
.这应该可以解决问题。
关于ruby-on-rails - 参数数量错误(2 对 1)-Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2076616/