我有一个联系表单设置并在我的“联系”页面上工作。但是,当我将该表单复制到另一个页面时,我收到此错误:“表单中的第一个参数不能包含nil或为空”。
这是我的contactcontroller:
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(params[:contact])
if @contact.valid?
ContactMailer.contact_email(@contact).deliver_now
redirect_to new_contact_path, notice: "Your email has been sent. Thank you."
else
render :new
end
end
end
这是另一个页面控制器:
class GolfcoursesController < ApplicationController
protect_from_forgery
def index
@golf_courses = GolfCourse.all
end
def show
@golf_courses = GolfCourse.all
@golfcourse = GolfCourse.find_by(slug: params[:slug])
@holes = @golfcourse.golf_holes
end
def events
end
def membership
end
def practice_facilities
end
def contact
end
def golf
@golf_courses = GolfCourse.all
end
def new
@contact = Contact.new
end
def create
@contact = Contact.new(params[:contact])
if @contact.valid?
ContactMailer.contact_email(@contact).deliver_now
redirect_to new_contact_path, notice: "Your email has been sent. Thank you."
else
render :new
end
end
end
以下是表格:
<div class="container">
<h4>Let us know if you have any questions.</h4>
<%= form_for @contact, :html => {:role => 'form'} do |f| %>
<div class="form-group">
<%= f.label :name, 'Enter your name:' %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email, 'Email:' %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :message, 'Message:' %>
<%= f.text_area :message, class: 'form-control', :rows => 3 %>
</div>
<div class="form-group">
<%= f.submit 'Submit', class: 'btn btn-default' %>
</div>
<% end %>
</div>
提前感谢您的帮助!
答案 0 :(得分:0)
&#39;表格中的第一个参数不能包含nil或为空&#39;
您还应该初始化其他控制器中的@contact
。
如果您想分享表格,我建议使用局部分和传入当地人。这使你的代码也有点干。
供参考,另请参阅导轨指南: http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials