没有检查表格参数不工作

时间:2014-12-03 20:57:34

标签: ruby-on-rails

我已经集成了一个名为nickname的隐藏字段,不应该输入,如果是,则表明机器人正在填写它,因此不应该处理动作邮件。

这是我必须执行此过滤器的代码:

if params[:nickname] == nil
  @contact = Contact.create(contact_params)
  if @contact.save
    GenericMailer.contact_mailer(@contact).deliver
  else
    redirect_to root_path alert: "Oops! It looks like you have entered something incorrectly, please try again."
  end
else
  redirect_to root_path
end

形式:

<%= form_for @contact, :url => {:controller => "footerlinks", :action => :contactcreate } do |lead|%>
    <%= lead.text_field :fullname, :placeholder => "Full name", onfocus: "this.placeholder = ''", onblur: "this.placeholder = 'Full name'" %>
    <%= lead.text_field :email, :placeholder => "Email", onfocus: "this.placeholder = ''", onblur: "this.placeholder = 'Email'" %>
    <%= lead.text_area :query, class: "height15", :placeholder => "Query", onfocus: "this.placeholder = ''", onblur: "this.placeholder = 'Query'" %>
    <%= lead.text_field :nickname %>
    <%= lead.submit "Send" %>
<% end %>

然而,在重新尝试表单时,它似乎完全绕过if语句并发送电子邮件,尽管昵称表单字段不是nil。

2 个答案:

答案 0 :(得分:1)

params[:nickname]可能不是nil而是空字符串。尝试params[:nickname].blank?

答案 1 :(得分:1)

你的问题是params[:nickname]甚至没有,这意味着它是零, 你有params[:contact][:nickname]

尝试

params[:contact][:nickname].blank? # instead of params[:nickname]