加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 错误的参数个数(2个为1) – Rails

发布时间:2020-12-17 02:50:39 所属栏目:百科 来源:网络整理
导读:我现在真的感到非常困惑和烦恼. 我正在运行Rails 2.3.5 我的观看/ pages / customers.html.erb只有: % form_tag do % First Name %= text_field_tag :firstName,params[:firstName] % Last Name %= text_field_tag :lastName,params[:lastName] % %= submit
我现在真的感到非常困惑和烦恼.

我正在运行Rails 2.3.5

我的观看/ 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 [:姓]).这应该可以解决问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读