Ruby on Rails 参数数量错误(给定 0 预期 1)

Posted

技术标签:

【中文标题】Ruby on Rails 参数数量错误(给定 0 预期 1)【英文标题】:Ruby on Rails Wrong Number of arguments (given 0 expected 1) 【发布时间】:2021-12-07 20:45:26 【问题描述】:

你好,我正在尝试使用 Ruby on Rails 模型创建新记录,但是我收到了错误的参数错误。

错误信息:

config/routes.rb

Rails.application.routes.draw do

mount RailsAdmin::Engine => '/admin', as: 'rails_admin' devise_for :修改 devise_for :用户 资源:user_steps 资源:离婚步骤 资源:离婚

root 'nav_pages#home'
get '/home', to:'nav_pages#home'
get '/wwd', to:'nav_pages#wwd'
get '/about', to:'nav_pages#about'
get '/contact', to:'nav_pages#contact'
get '/blog', to:'nav_pages#blog'
get '/are_you_married', to: 'qualifier#are_you_married'
get '/want_a_divorce', to: 'qualifier#want_a_divorce'

结束

divorces_controller.rb:

class DivorcesController < ApplicationController

定义新 @离婚=离婚.new 结束

定义创建 @divorce = Divorce.new(user_params) @divorce.save 结束

私人

def user_params
  params.require.(:divorce).permit(:marriage_date, :seperation_date, :state_of_mariage, :child_support, :address, :childrens_address, :contact_with_other, :telephone)
end

结束

new.html.erb

<div class = "container top-cont">
太好了,让我们开始约会吧!

  <%= f.label :marriage_date %>
  <%= f.text_field :marriage_date, class: 'form-control' %>
  
  <%= f.label :seperation_date %>
  <%= f.text_field :seperation_date, class: 'form-control' %>
  
  <%= f.label :state_of_marriage %>
  <%= f.text_field :state_of_marriage, class: 'form-control' %>
  
  <%= f.label :child_support%>
  <%= f.text_field :child_support, class: 'form-control' %>
  
  
<%= f.label :child_support %>
  <%= f.text_field :child_support, class: 'form-control' %>
  
      <%= f.label :address %>
  <%= f.text_field :address, class: 'form-control' %>
  
      <%= f.label :childrens_address %>
  <%= f.text_field :childrens_address, class: 'form-control' %>
  
<%= f.label :contact_with_other %>
  <%= f.text_field :contact_with_other, class: 'form-control' %>
  
      <%= f.label :telephone %>
  <%= f.text_field :telephone, class: 'form-control' %>
  
  <%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>

非常感谢您的帮助!

【问题讨论】:

【参考方案1】:

尝试:

params.require(:divorce)

其实强参数使用语法:

params.require(:modal) 

【讨论】:

【参考方案2】:

问题出在divorces_controller.rb

应该是params.require(:divorce),而不是params.require.(:divorce)

【讨论】:

哇,这对我来说是一个非常愚蠢的错误!!!!它修复了一切。非常感谢!!!!

以上是关于Ruby on Rails 参数数量错误(给定 0 预期 1)的主要内容,如果未能解决你的问题,请参考以下文章

Rail6 db:migrate 失败 - 参数数量错误(给定 1,预期 0)

Rails 2.4 => 3.0:ActiveRecord:`add_modifier`:参数数量错误(给定 3,预期 2)(ArgumentError)

mass_assignment_authorizer 的更改导致 Ruby on Rails 3.1 中的错误

ruby 3和rails 6的watir:参数数量错误

Ruby on rails 中的捆绑错误

ArgumentError:升级 Rails 6.1.3 时出现错误数量的参数错误