【问题标题】:wrong number of arguments (0 for 1..2) Rails 4.2.0 and Ruby 2.2.2参数数量错误(0 表示 1..2) Rails 4.2.0 和 Ruby 2.2.2
【发布时间】:2015-06-27 14:35:39
【问题描述】:

我正在尝试将 mail_form 嵌入到我的主页中。

“contacts#new”在“static_pages#home”上呈现,因此我在 StaticPagesController 的 home 操作中实例化 @contact,以避免“表单中的第一个参数不能包含 nil 或为空”错误。

但我现在得到错误数量的参数(0 代表 1..2)错误。

自从我将 ruby​​ 更新到 2.2.2 后,我看到了这种行为。

您认为这个错误是由 Ruby 的新规范引起的,还是只是我的简单代码错误?

static_pages_controller

class StaticPagesController < ApplicationController

def home
  @contact = Contact.new
end

end

views/static_pages/home.html.erb

<section id="contact">
  <%= render template: 'contacts/new' %>
</section>

contacts_controller

class ContactsController < ApplicationController

def new
  @contact = Contact.new
end

def create
  @contact = Contact.new(contact_params)
  @contact.request = request

  if @contact.deliver
    flash.now[:notice] = "Thank you for your message. We will contact you soon!"
  else
    flash.now[:error] = "Cannot send message."
    render 'new'
  end
end

private

def contact_params
  params.require(:contact).permit(:name, :email, :message)
end

end

views/contacts/new.html.erb

<%= form_for(@contact) do |f| %>
...
<% end %>

日志

Processing by StaticPagesController#home as HTML
Rendered contacts/new.html.erb (23.5ms)
Rendered static_pages/home.html.erb within layouts/application (238.2ms)
Completed 500 Internal Server Error in 278ms

ArgumentError (wrong number of arguments (0 for 1..2)):
app/views/contacts/new.html.erb:28:in `block in_app_views_contacts_new_html_erb___4238619170782302276_70113984753480'

【问题讨论】:

  • 澄清一下:错误发生在@contact = Contact.new ?
  • @ma_il :是的,看起来是这样。我也尝试在“联系人/新”页面上,但它给了我同样的错误。一定是我指定的ruby版本或者Gem set有问题。

标签: ruby-on-rails-4.2 ruby-2.2


【解决方案1】:

我将我的“byebug”版本更新为“4.0.1”,将“Web-console”更新为“2.1.2”,然后它会显示错误在哪里。其实真的很蠢……

错误:

<%= form_for(@contact) do |f| %>

<%= t.email_field ... %>
...
<% end %>

正确:

<%= form_for(@contact) do |f| %>

<%= f.email_field ... %>
...
<% end %>

【讨论】:

  • 好的,这部分不在问题中;)玩得开心你的应用程序。
  • @ma_il :是的,我很抱歉。直到'bybug'向我指出,我才意识到这一点。下次我会更加小心的。
猜你喜欢
  • 2015-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多