参数缺失或值为空:联系人

param is missing or the value is empty: contacts

我正在尝试提交我的联系表,该表应该可以发送电子邮件。每次单击提交按钮时,我都会收到此错误:"param is missing or the value is empty: contacts".

我的routes.rb:

Rails.application.routes.draw do

  resources :contacts

  root to: 'static#actor_list'

  get 'static/actor_list'

  get 'actor' => 'static#actor'

  get 'contact' => 'static#contact'

end

我的contacts_controller.rb:

class ContactsController < ApplicationController
  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(contact_params)

    if @contact.valid?
      ContactMailer.contact_email(@contact).deliver_now
      redirect_to(new_contact_path, notice: 'Your query was sent.')
    else
      render :new
    end
  end

  def show
  end

  private
    def contact_params
      params.require(:contact).permit(:name, :email, :subject, :message)
    end
end

contact.rb:

class Contact
  include ActiveModel::Model
  attr_accessor :name, :email, :subject, :message
  validates_presence_of :name, :subject, :message
  validates :email, presence: true, email: true
end

查看:

<!-- scrollToTop -->
<!-- ================ -->
<div class="scrollToTop"><i class="icon-up-open-big"></i></div>

<!-- page wrapper start -->
<!-- ================ -->
<div class="page-wrapper">

  <!-- page-intro start-->
  <!-- ================ -->
  <div class="page-intro">
    <div class="container">
      <div class="row">
        <div class="col-md-12">
          <ol class="breadcrumb">
            <li><i class="fa fa-home pr-10"></i><a href="index.html">Home</a></li>
            <li class="active">Contact</li>
          </ol>
        </div>
      </div>
    </div>
  </div>
  <!-- page-intro end -->

  <!-- main-container start -->
  <!-- ================ -->
  <section class="main-container">

    <div class="container">
      <div class="row">

        <!-- main start -->
        <!-- ================ -->
        <div class="main col-md-12">

          <!-- page-title start -->
          <!-- ================ -->
          <h1 class="page-title">Contact Us</h1>
          <!-- page-title end -->
          <div class="row">
            <div class="col-md-6">
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do tempor.</p>
              <div class="alert alert-success hidden" id="MessageSent">
                We have received your message, we will contact you very soon.
              </div>
              <div class="alert alert-danger hidden" id="MessageNotSent">
                Oops! Something went wrong please refresh the page and try again.
              </div>
              <div class="contact-form">
                  <%= @contact.errors.full_messages.join(', ') %>
                  <%= form_for @contact, id: 'contact-form' do |f| %>

                    <div class="form-group has-feedback">
                      <%= f.label :name %>
                      <%= f.text_field :name, :class => 'form-control', :id => 'name', :name => 'name' %>
                      <i class="fa fa-user form-control-feedback"></i>
                    </div>

                    <div class="form-group has-feedback">
                      <%= f.label :email %>
                      <%= f.text_field :email, :class => 'form-control', :id => 'email', :name => 'email' %>
                      <i class="fa fa-envelope form-control-feedback"></i>
                    </div>

                    <div class="form-group has-feedback">
                      <%= f.label :subject %>
                      <%= f.text_field :subject, :class => 'form-control', :id => 'subject', :name => 'subject' %>
                      <i class="fa fa-navicon form-control-feedback"></i>
                    </div>

                    <div class="form-group has-feedback">
                      <%= f.label :message %>
                      <%= f.text_area :message, :class => 'form-control', :rows => '6', :id => 'message', :name => 'message' %>
                      <i class="fa fa-pencil form-control-feedback"></i>
                    </div>

                    <%= f.submit 'Submit Query', :class => 'submit-button btn btn-default' %>

                  <% end %>
              </div>
            </div>
            <div class="col-md-6">
              <!-- google maps start -->
              <div id="map-canvas"></div>
              <!-- google maps end -->
            </div>
          </div>
        </div>
        <!-- main end -->

      </div>
    </div>
  </section>
  <!-- main-container end -->

</div>
<!-- page-wrapper end -->

<!-- JavaScript files placed at the end of the document so the pages load faster
================================================== -->
<!-- Google Maps javascript -->
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;sensor=false"></script>

日志:

  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (57.0ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/_markup.html.erb (0.3ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/console.js.erb within layouts/javascript (44.6ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.3ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/index.html.erb (99.5ms)


Started POST "/contacts" for ::1 at 2015-05-07 17:52:21 +0200
Processing by ContactsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"1vM4XYHNV18FBTzlSGROW7HQx37R3YHVPnlCOI/9RJZo2NsL8UitcbJNv6ileVKNSaI9fIxNIRGQK8JghztJjA==", "name"=>"fasd", "email"=>"fasdf@gmail.com", "subject"=>"dasdf", "message"=>"fasdf", "commit"=>"Submit Query"}
Completed 400 Bad Request in 6ms (ActiveRecord: 0.0ms)

ActionController::ParameterMissing (param is missing or the value is empty: contact):
  app/controllers/contacts_controller.rb:22:in `contact_params'
  app/controllers/contacts_controller.rb:7:in `create'


  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (6.0ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.2ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.7ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (69.2ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/_markup.html.erb (0.6ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.4ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/console.js.erb within layouts/javascript (61.1ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.3ms)
  Rendered /Users/maahd/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/web-console-2.1.2/lib/web_console/templates/index.html.erb (129.1ms)

由于您的 contact_params 需要 :contact,它希望 :name:email:subject:message 进入联系人参数,但是当您在对象中设置 name 属性时,您将覆盖它。

如果您不设置它,Rails 将自动将其设置为,例如 contact[name],一切都会正常工作。只需删除您字段​​中的 name

Here 您可以看到 text_field 的行为方式以及它生成的 HTML 类型,您可以看到它设置的 name