rails表单跳过验证并且不保存

时间:2013-08-11 21:23:20

标签: ruby-on-rails forms validation

我的rails表单跳过验证并且不保存。关于为什么会发生这种情况的任何想法?

最终发生的是表单提交并进入创建操作,但它会跳过在contact.rb中完全设置的验证,并且联系人控制器的@contact.save中的这一行不会保存,它会继续到了别的地方。

这是在使用虚拟数据提交联系表单后在终端服务器窗口中打印的内容:

logged in #<Contact:0xb60b3978>


Started POST "/contacts" for 127.0.0.1 at 2013-08-11 17:04:46 -0400
Processing by ContactsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"zbEGKGXz1t2Os6VjVOMzcfeHru9sBfoBaF6tgr16qPo=", "contact"=>{"name"=>"", "email"=>"", "subject"=>"", "body"=>""}, "category"=>"bug", "commit"=>"Contact Us"}
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 3]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 3]]
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
Redirected to http://localhost:3000/contact
Completed 302 Found in 9ms (ActiveRecord: 0.3ms)


Started GET "/contact" for 127.0.0.1 at 2013-08-11 17:04:46 -0400
Processing by ContactsController#new as HTML
  Rendered common/_form_errors.html.erb (0.0ms)
  Rendered contacts/_contact_form.html.erb (2.3ms)
  Rendered contacts/new.html.erb within layouts/application (2.9ms)
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 3]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 3]]
  Rendered common/_search_form.html.erb (0.5ms)
  Rendered layouts/_navbar.html.erb (4.2ms)
  Rendered layouts/_flashmessages.html.erb (0.1ms)
  Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 72ms (Views: 71.2ms | ActiveRecord: 0.2ms)

Contact.rb:

class Contact < ActiveRecord::Base
    belongs_to :user    

    attr_accessible :body, :category, :email, :name, :subject

    validates :body, presence: true, length: { in: 10..255 }

    validates :category, presence: true

    validates :name, presence: true

    validates :email, presence: true

    validates :subject, presence: true


end

_contact_form.html.erb:

<%= form_for(@contact) do |f| %>

    <%= render 'common/form_errors', object: @contact %>
        <div class="span4">
            <div class="well">
                <%= f.label :name, "What's your name?" %> 

                <%= f.text_field :name, :class => "field span4" %>
            </div>

            <div class="well">

                <%= f.label :email, "Your Email so we can hit you back." %> 
                <%= f.text_field :email, :class => "field span4" %>
            </div>

            <div class="well">
                <%= f.label :category, "Pick one:" %> 
                <%= select_tag(:category, 
                options_for_select(
                [['I found a bug.', 'bug'], ['I have a suggestion.', 'suggestion'], ['other', 'other']]
                )) %>
            </div>

            <div class="well">
                <%= f.submit "Contact Us", :class=> "marginTopBottom" %> 
            </div>
        </div>

        <div class="span4">
            <div class="well">
                <%= f.label :subject, "Subject:" %> 

                <%= f.text_field :subject, :class => "field span4" %>
            </div>

            <div class="well">
                <%= f.label :body, "What's Up?" %> 

                <%= f.text_area(:body, :rows => 7, :class => "field span4") %>
            </div>


        </div>
<% end %>

触点/ new.html.erb:

<%= provide(:navActive, 'Contact Us') %>
<h1>Contact Us</h1>

<div class="row">
    <%= render 'contact_form' %>
</div>

contacts_controller.rb:

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

  def create
    if logged_in?
      @contact = current_user.contacts.build(params[:contact])
      puts "logged in #{@contact}" # prints out when logged in
    else 
      @contact = Contact.new(params[:contact])
      @contact.user_id = 999999
      puts "not logged in #{@contact}" # prints out when not logged in
    end

    if @contact.save
        flash[:success] = "Your message was sent and we'll get back to you as soon as possible!"

        # send an email to offering poster about the new request
        UserMailer.contact(@contact, sent_at = Time.now).deliver
        redirect_to contact_path
    else
      flash[:success] = "Your message was not sent. Something went wrong. Please contact pavankat@gmail.com."
      redirect_to contact_path
    end

  end
end

的routes.rb

  resources :contacts, only: [:new, :create] 

schema.rb:

  create_table "contacts", :force => true do |t|
    t.string   "name"
    t.string   "email"
    t.string   "category"
    t.string   "subject"
    t.string   "body"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.integer  "user_id"
  end

  add_index "contacts", ["user_id"], :name => "index_contacts_on_user_id"

2 个答案:

答案 0 :(得分:0)

我认为它不会跳过验证,我认为您的页面无法显示错误。

您需要在页面的某处添加<%= flash[:success] %>

您还可以输出contact对象的错误,这可能有所帮助!

(您可以通过发布common/form_errors -

的内容来反驳这一点

答案 1 :(得分:0)

应该是:

<%= f.select(:category, 
                options_for_select(
                [['I found a bug.', 'bug'], ['I have a suggestion.', 'suggestion'], ['other', 'other']]
                )) %>