为什么我的验证无法在rails app中运行? (Daniel Kehoe的学习Rails教程书)

时间:2018-03-12 12:41:02

标签: ruby-on-rails learn-ruby-on-rails

我在Daniel Kehoe的学习轨道书的第19章,并创建了一个表格。但是,当我故意制作错误以便测试验证时,不会出现任何消​​息。例如,当我提交一个空表单时,它只是呈现一个没有错误消息的新表单,当我输入错误的电子邮件地址时,没有' @'再次它似乎接受我的输入没有错误。所以我的问题是为什么验证不会引发任何错误信息?

这是我的模型contact.rb

class Contact
    include ActiveModel::Model
    attr_accessor :name, :email, :content

    validates_presence_of :name
    validates_presence_of :email
    validates_presence_of :content
    validates_format_of :email,
        with: /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
    validates_length_of :content, :maximum => 500
end

应用程序布局application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%= content_for?(:title) ? yield(:title) : "Workspace" %></title>
    <meta name="description" content="<%= content_for?(:description) ? yield(:description) : "Workspace" %>">
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => 'reload' %>
    <%= csrf_meta_tags %>
  </head>
  <body>
    <header>
      <%= render 'layouts/navigation' %>
    </header>
    <main role="main">
      <%= render 'layouts/messages' %>
      <%= yield %>
    </main>
  </body>
</html>

表单视图new.html.erb

<% content_for :title do %>Contact<% end %>
<h3>Contact</h3>
<div class="form">
    <%= form_with(model: @contact) do |form| %>
        <%= form.label :name %>
        <%= form.text_field :name, autofocus: true %>
        <br/>
        <br/>
        <%= form.label :email %>
        <%= form.email_field :email %>
        <br/>
        <br/>
        <%= form.label 'message' %>
        <%= form.text_area :content, size: '40x5' %>
        <br/>
        <br/>
        <%= form.submit 'Submit', class: 'submit' %>
    <% end %>
</div>

contacts_controller.rb

    class ContactsController < ApplicationController

  def new
  @contact = Contact.new
  end

  def create
    @contact = Contact.new(secure_params)
    if @contact.valid?
      # TODO send message
      flash[:notice] = "Message sent from #{@contact.name}."
      redirect_to root_path
    else
      render :new
    end
  end

  private
    def secure_params
      params.require(:contact).permit(:name, :email, :content)
    end
end

布局助手_messages.html.erb

<%# Rails flash messages styled for Bootstrap 3.0 %>
<% flash.each do |name, msg| %>
  <% if msg.is_a?(String) %>
    <div class="alert alert-dismissible alert-<%= name.to_s == 'notice' ? 'success' : 'danger' %>">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
      <%= content_tag :div, msg, :id => "flash_#{name}" %>
    </div>
  <% end %>
<% end %>

编辑1:

当我在控制台中手动创建新联系人时,验证似乎有效。但是,应用程序在通过应用程序表单创建时未验证它们。请参阅下面有关创建新联系人的实验。

2.3.4 :002 > c=Contact.new
 => #<Contact:0x000000055741a8> 
2.3.4 :003 > c
 => #<Contact:0x000000055741a8> 
2.3.4 :004 > c.name='biggles2'
 => "biggles2" 
2.3.4 :005 > c
 => #<Contact:0x000000055741a8 @name="biggles2"> 
2.3.4 :006 > c.valid?
 => false 
2.3.4 :007 > c.email='biggldy@boggldy.com'
 => "biggldy@boggldy.com" 
2.3.4 :008 > c
 => #<Contact:0x000000055741a8 @name="biggles2", @validation_context=nil, @errors=#<ActiveModel::Errors:0x0000000554a948 @base=#<Contact:0x000000055741a8 ...>, @messages={:email=>["can't be blank", "is invalid"], :content=>["can't be blank"]}, @details={:email=>[{:error=>:blank}, {:error=>:invalid, :value=>nil}], :content=>[{:error=>:blank}]}>, @email="biggldy@boggldy.com"> 
2.3.4 :009 > c.email
 => "biggldy@boggldy.com" 
2.3.4 :010 > c.name
 => "biggles2" 
2.3.4 :011 > c.content='this is some content and its not too long'
 => "this is some content and its not too long" 
2.3.4 :012 > c.valid?
 => true 
2.3.4 :013 > c
 => #<Contact:0x000000055741a8 @name="biggles2", @validation_context=nil, @errors=#<ActiveModel::Errors:0x0000000554a948 @base=#<Contact:0x000000055741a8 ...>, @messages={}, @details={}>, @email="biggldy@boggldy.com", @content="this is some content and its not too long"> 

0 个答案:

没有答案