【发布时间】: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