我的rails应用程序出错了。 param缺失或值为空:联系
家庭控制器
$(document).ready(function() {
$('.datepicker').each(function(){
var pickr = $(this).pickadate({
format: 'dd/mm/yyyy',
selectMonths: true, // Creates a dropdown to control month
selectYears: 5, // Creates a dropdown of 15 years to control year
editable: true
});
$(this).click(function(){
pickr.pickadate('open');
});
});
$('select').material_select();
});
邮件程序/表格的
def contact
@contact = Contact.new(contact_params)
if @contact.save
redirect_to root_path
firstname = params[:contact][:firstname]
lastname = params[:contact][:lastname]
email = params[:contact][:email]
message = params[:contact][:message]
ContactMailer.contact_email(firstname, lastname, email, message).deliver
flash[:success] = "Thanks for your message, we will be in touch soon."
else
redirect_to home_contact_path
flash[:danger] = "Opps, there was a problem! Please fill out all the fields."
end
end
private
def contact_params
params.require(:contact).permit(:firstname, :lastname, :email, :message)
end
联系页面
<div class="contact-form">
<%= simple_form_for @contact do |f| %>
<%= f.input :firstname, required: true %>
<%= f.input :lastname, required: true %>
<%= f.input :email, required: true %>
<%= f.input :message, required: true %>
<%= f.submit "Get in touch!", class: "btn btn-info" %>
<% end %>
</div>
Contact.rb
<!DOCTYPE html>
<div class="jumbotron">
<h1 class="center-aligned">Contact Me</h1>
</div>
<div class="main-content">
<p class="center-aligned">You can contact me via the social media links at the bottom of the site or via the contact form below.</p>
<%= render 'home/mailer' %>
</div>
服务器日志
class Contact < ActiveRecord::Base
validates_presence_of :firstname, :lastname, :email, :message
end
如果您还有其他需要,请告诉我。我还在学习Ruby on Rails。
答案 0 :(得分:1)
渲染表单时,您没有定义@contact
。
呈现表单的操作需要类似@contact = Contact.new
另一方面,您需要定义一个POST方法来提交表单。联系人应该是你的帖子方法,其中表单被提交,你需要另一个动作GET与我之前给你的代码。
def contact
@contact = Contact.new(contact_params)
end
def create
if @contact.save
redirect_to root_path
firstname = params[:contact][:firstname]
lastname = params[:contact][:lastname]
email = params[:contact][:email]
message = params[:contact][:message]
ContactMailer.contact_email(firstname, lastname, email, message).deliver
flash[:success] = "Thanks for your message, we will be in touch soon."
else
redirect_to home_contact_path
flash[:danger] = "Opps, there was a problem! Please fill out all the fields."
end
end
的routes.rb
resources :contacts
答案 1 :(得分:0)
只是你的表单参数是空的,从那里得到错误的原因。此外,您还必须定义路线。
表格
<%= simple_form_for Contact.new, url: :contact_emailer do |f| %>
<强>路由强>
post 'contact_emailer' => "contacts#contact", as: :contact_emailer
答案 2 :(得分:-1)
您收到此错误是因为您尝试在此处指定params:
@contact = Contact.new(contact_params)
并且由于尚未提交任何内容,并且您指定了params.require(:contact)
,您会收到错误消息。
以下是你做的事情:作为一个好的铁路公民创建单独的方法来显示表单(通常为new
),另一个用于提交表单(通常是create
)
看起来有点像这样:
def new
@contact = Contact.new # this is empty model, no data yet
end
def create
@contact = Contact.new(contact_params)
# ... and all the other fancy stuff you have there
end
不要忘记将您的观看文件名更新为new.html.erb
或致电控制器中的render
。
可以使用一种方法在提交时显示新表单和处理请求,但与new / create的分离更清晰。