我正在开发一个简单的Ruby on Rails单页网站,该网站上有一个'联系我们'形成。它有3个字段 - 名称,电子邮件和文本 - 以及提交按钮。理论上,在填写这些表单并单击按钮后,用户将收到一封确认请求的电子邮件。控制器基本上只有一种方法可以将数据写入数据库,成功保存后会发送一封电子邮件。
问题是控制器没有收到参数(姓名,电子邮件和文本)并引发异常:ActionController::ParameterMissing (param is missing or the value is empty: application)
我确定数据已发送。这是日志:
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
控制器的源代码:
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
应用程序模型的源代码:
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
有谁知道问题出在哪里以及如何解决?
提前谢谢。
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>
答案 0 :(得分:1)
您的params
不是来自application
,为了快速解决,只需将application_params
更改为
def application_params
params.permit(:name, :email, :text)
end