我在我的rails應用程序中出現錯誤。 參數是丟失或爲空值:接觸Param缺少錯誤 - Ruby on 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。
u能張貼服務器日誌錯誤 – 7urkm3n
Server日誌已經被添加到原來的職位。 – Andy
在前面的控制檯中,您應該看到發送給服務器的參數,請將其放在此處。 – ppascualv