【发布时间】:2016-05-18 03:55:36
【问题描述】:
我的 rails 应用程序出现错误。 参数缺失或值为空:联系人
家庭控制器
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>
联系页面
<!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>
Contact.rb
class Contact < ActiveRecord::Base
validates_presence_of :firstname, :lastname, :email, :message
end
服务器日志
Started GET "/home/contact" for 127.0.0.1 at 2016-05-17 14:13:13 -0500
Processing by HomeController#contact as HTML
Completed 500 in 6ms (ActiveRecord: 0.0ms)
ActionController::ParameterMissing (param is missing or the value is empty: contact):
app/controllers/home_controller.rb:28:in `contact_params'
app/controllers/home_controller.rb:9:in `contact'
Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (21.9ms)
Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (9.8ms)
Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (3.5ms)
Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (80.8ms)
Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (1.9ms)
Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (1.3ms)
Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (1.8ms)
Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (1.9ms)
Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (72.9ms)
Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (1.3ms)
Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (2.0ms)
Rendered /home/andy/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (135.5ms)
如果您需要其他任何东西,请告诉我。我还在学习 Ruby on Rails。
【问题讨论】:
-
你可以发布服务器日志错误
-
服务器日志已添加到原帖中。
-
在此行之前的控制台中,您应该看到发送到服务器的参数,请将其放在这里。
-
请求后添加所有内容。
-
您在显示表单之前设置了
@contact对象吗?如您所见,您有simple_form_for @contact,它需要一个对象@contact,因此在显示您的联系页面的操作中,您必须设置@contact = Contact.new
标签: ruby-on-rails ruby