2013-01-16 75 views
2

我正在一個簡單的項目中保存用戶。我遵循Ruby on Rails教程及其步驟,但我陷入了顯示驗證錯誤消息的困境。當我點擊「創建我的帳戶」按鈕而不填寫空白時,直接重定向到/用戶。但我看不到驗證錯誤。代碼如下圖所示:爲什麼驗證錯誤消息不顯示?

user.rb

class User < ActiveRecord::Base 
    has_secure_password 

    attr_accessible :name, :email, :password, :password_confirmation 

    before_save { |user| user.email = email.downcase } 

    validates :name, presence: true, length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, 
        format:  { with: VALID_EMAIL_REGEX }, 
        uniqueness: { case_sensitive: false } 
    validates :password, presence: true, length: { minimum: 6 } 
    validates :password_confirmation, presence: true 
end 

UserController中:

class UsersController < ApplicationController 
    def show 
    @user = User.find(params[:id]) 
    end 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(params[:user]) 
    if @user.save 
     redirect_to @user 
    else 
     render 'new' 
    end 
    end 
end 

new.html.erb

<% provide(:title, 'Sign up') %> 
    <h1>Sign up</h1> 

    <div class="row"> 
    <div class="span6 offset3"> 
     <%= form_for(@user) do |f| %> 
     <%= render 'shared/error_messages' %> 
     <%= f.label :name %> 
     <%= f.text_field :name %> 
     <%= f.label :email %> 
     <%= f.text_field :email %> 

     <%= f.label :password %> 
     <%= f.password_field :password %> 

     <%= f.label :password_confirmation, "Confirmation" %> 
     <%= f.password_field :password_confirmation %> 

     <%= f.submit "Create my account", class: "btn btn-large btn-primary" %> 
     <% end %> 
    </div> 
    </div> 

application.html.erb

<!DOCTYPE html> 
    <html> 
    <head> 
     <title><%= full_title(yield(:title)) %></title> 
     <%= stylesheet_link_tag "application", media: "all" %> 
     <%= javascript_include_tag "application" %> 
     <%= csrf_meta_tags %> 
     <%= render 'layouts/shim' %>  
    </head> 
    <body> 
     <%= render 'layouts/header' %> 
     <div class="container"> 
     <% flash.each do |key, value| %> 
      <div class="alert alert-<%= key %>"><%= value %></div> 
     <% end %> 
     <%= yield %> 
     <%= render 'layouts/footer' %> 
     <%= debug(params) if Rails.env.development? %> 
     </div> 
    </body> 
    </html> 

的routes.rb

SampleApp::Application.routes.draw do 

    root to: 'static_pages#home' 

    match '/signup', to: 'users#new' 
    match '/help',to: 'static_pages#help' 
    match '/about',to:'static_pages#about' 
    match '/contact',to:'static_pages#contact' 
    match "/users", to:'users#showall' 
    match '/users/:id', to: 'users#show', as: 'user' 
end 

_error_messages.html.erb

<% if @user.errors.any? %> 
<div id="error_explanation"> 
    <div class="alert alert-error"> 
    The form contains <%= pluralize(@user.errors.count, "error") %>. 
    </div> 
    <ul> 
    <% @user.errors.full_messages.each do |msg| %> 
    <li>* <%= msg %></li> 
    <% end %> 
    </ul> 
</div> 
<% end %> 
+0

成規必須有鑑於/共享文件夾的部分稱爲error_messages,ü可以張貼的來源,以及,怎麼好像你的錯誤是從局部 – sameera207

+0

打印是的,我現在所擁有的部分和IM增加。 – pegatron

+0

確保在您的config/environment.rb(或application.rb或任何您使用的文件)中設置了「config.consider_all_requests_local = true」。這將啓用調試。 – Automatico

回答

0

我認爲 「更新 - 發現的解決方案」在下面的SO問題的底部也可以幫助你。

Rails render route path

正如sameera207提到,宣稱「用戶」當你爲該資源定義的所有正確的路由資源。

相關問題