简体   繁体   English

Rails没有收到http发布请求参数

[英]Rails doesn't receive http post request parameters

I'm working on a simple one-page website on Ruby on Rails that has a 'Contact Us' form. 我正在Ruby on Rails上一个简单的单页网站上工作,该网站具有“联系我们”表格。 It has 3 fields - name, e-mail and text -, and a submit button. 它具有3个字段-名称,电子邮件和文本-以及一个提交按钮。 In theory, after filling up these forms and clicking the button, the user will receive an email with the confirmation of the request. 从理论上讲,填写完这些表格并单击按钮后,用户将收到一封电子邮件,其中包含对请求的确认。 The controller basically has only one method that writes the data in the DB and after successful saving sends an email. 控制器基本上只有一种将数据写入DB的方法,成功保存后会发送电子邮件。

The problem is that the controller doesn't receive the parameters(name, email and text) and raises an exception: ActionController::ParameterMissing (param is missing or the value is empty: application) 问题在于控制器未接收到参数(名称,电子邮件和文本),并引发了异常: ActionController::ParameterMissing (param is missing or the value is empty: application)

I'm sure that the data is sent. 我确定数据已发送。 Here's the log: 这是日志:

Started POST "/" for 127.0.0.1 at 2015-07-23 20:25:08 +0300
Processing by ApplicationsController#create as HTML
  Parameters: {"name"=>"Foo Bar", "email"=>"foobar@example.com", "text"=>"qwerqwepoiqtwporihtqwojbr[sdfzxc"}
Completed 400 Bad Request in 381609ms

Source code of the controller: 控制器的源代码:

class ApplicationsController < ApplicationController

  skip_before_filter :verify_authenticity_token, :if => Proc.new { |c| c.request.format == 'text/html' }

  def new
    @request = Application.new
  end

  def create
    @request = Application.new(application_params)
    if @request.save
      ApplicationMailer.request_confirmation(@request).deliver_now
      flash[:info] = 'Your application has been registered! Check your e-mail!'
    else
      flash[:alert] = 'Oops! Your application was not registered!'
    end
  end

  private
    def application_params
      params.require(:application).permit(:name, :email, :text)
    end
end

Source code of the Application model: 应用程序模型的源代码:

class Application < ActiveRecord::Base
  before_save{
    self.email.downcase!
  }
  validates(:name, presence: true, length: { maximum: 50 })
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates(:email, presence: true, length: {maximum: 255},
            format: {with: VALID_EMAIL_REGEX})
  validates(:text, presence: true)
end

Does anybody have any idea where is the problem and how to solve it? 是否有人知道问题在哪里以及如何解决?

Thank you in advance. 先感谢您。

UPD: Form code: UPD:表单代码:

<form method="post" class="form-horizontal col-md-8" name="application" id="applyForm">
        <div class="row" id="credentials">
          <div class="col-sm-6">
            <div class="input-group form-group" id="full_name">
              <span class="input-group-addon" id="basic-addon1"><i class="fa fa-user"></i></span>
              <input type="text" name="name" class="form-control" placeholder="Your name" aria-describedby="basic-addon1" id="name">
            </div>
          </div>
          <div class="col-sm-6">
            <div class="input-group form-group" id="email">
              <span class="input-group-addon" id="basic-addon1"><i class="fa fa-envelope"></i></span>
              <input type="text" name="email" class="form-control" placeholder="E-mail" aria-describedby="basic-addon1" id="email">
            </div>
          </div>
        </div>
        <div class="row" id="comment-area">
          <div class="col-sm-12" id="comment">
            <textarea class="form-control" name="text" rows="10" placeholder="Please describe your system" id="text"></textarea>
          </div>
        </div>
        <div class="row" id="submit-button">
          <button type="submit" class="btn btn-default"><i class="fa fa-rocket"></i>Send message</button>
        </div>
    </form>

Your params are not coming from application , for a quick fix just change application_params to below 您的params不是来自application ,为了快速解决,只需将application_params更改为以下

def application_params
  params.permit(:name, :email, :text)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM