无法在rails中进行验证

时间:2017-01-05 10:09:29

标签: ruby-on-rails ruby validation

我有一个联系我们页面

    class ContactPagesController < ApplicationController

    def new
    @contact_page =  ContactPage.new
     end

     def create


      @contact_page = ContactPage.new(contact_page_params)

      if    @contact_page.save
          flash[:notice]="details saved successfully"
     redirect_to contact_pages_path
     else
    flash[:notice]="details not saved"
    redirect_to contact_pages_path
      end
    #  if @contact_page.errors.any?
    # flash[:notice]="there are errors"
     # end
    end

       private
      def contact_page_params
      params.require(:contact_page).permit( :first_name, :last_name, :email,    :phone, :do_you_have_land, :moving_time_frame, :financing, :to$
       end
       end

和我的模特

    class ContactPage < ApplicationRecord
    validates :email, presence: true
    end

和视图

      new.html.erb

      <%= form_for @contact_page, :html => { :class => 'form-horizontal', multipart: true } do |f| %>
      <% if @contact_page.errors.any? %>
      <div id="error_explanation">
      <h3>

        <%= pluralize(@contact_page.errors.count, "error") %>
        prohibited this employee from being saved:
      </h3>
      <ul>
        <% @contact_page.errors.full_messages.each do |message| %>
          <li>
            <%= message %>
          </li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="control-group">
   <%= f.label :First_Name, :class => 'control-label' %>
    <div class="controls">
     <%= f.text_field :first_name, :class => 'text_field' %>
   </div>
   </div>
  <div class="control-group">
  <%= f.label :Email, :class => 'control-label' %>
   <div class="controls">
    <%= f.text_field :email, :class => 'text_field' %>
   </div>
  </div>
  <div class="control-group">
  <%= f.label :How_can_we_assist_you, :class => 'control-label' %>
  <div class="controls">
  <%= f.text_field :assist_you, :class => 'text_field' %>
  </div>
  </div>




 <div class="form-actions">
 <%= f.submit nil, :class => 'btn btn-primary' %>

 </div>
 <% end %>

如果填写了所有字段,则数据将保存到数据库中。如果留下电子邮件,则数据库中也没有保存空白数据(由于验证),但错误消息也未呈现。 @ contact_page.errors.any?由于某种原因,视图页面内部被忽略。我预计会出现错误&#34;电子邮件不能为空白&#34;在表格的顶部。

为什么保存操作不会触发验证错误?

我的联系页面路线

       get 'contact_pages' => 'contact_pages#new', :as => 'contact_pages'
       post 'contact_pages' => 'contact_pages#create'

在我出错的地方需要帮助。非常感谢任何帮助。谢谢。

2 个答案:

答案 0 :(得分:1)

你应该用户渲染错误而不是重定向

def create
    @contact_page = ContactPage.new(contact_page_params)

    if    
       @contact_page.save
       flash[:notice]="details saved successfully"
       redirect_to contact_pages_path
    else
       flash[:notice]="details not saved"
       render :new
    end
end

答案 1 :(得分:1)

class SaveController < ApplicationController

         def new
           @contact_page =  ContactPage.new
         end 

    def create
        if    
           @contact_page.save
           flash[:notice]="Successfuly stored"
           redirect_to contact_pages_path
        else
           flash[:notice]="please check your inputs"
           render :new
        end
    end
end

在视图中: -

 <% if notice  %>
            <div class="alert alert-success alert-dismissible" role="alert">
              <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <%= notice %>
            </div>
  <% end %>