2

ユーザーを救う単純なプロジェクトに取り組んでいます。Ruby on Rails チュートリアルとその手順に従っていますが、検証エラー メッセージの表示について行き詰まりました。空欄を埋めずに「Create my account」ボタンをクリックすると、直接 /users にリダイレクトされます。しかし、検証エラーは表示されませんでした。以下のようなコード:

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+\-.]+@[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

ユーザーコントローラー:

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>

ルート.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 %>
4

1 に答える 1

0

次の SO 質問の下部にある「更新 - 解決策が見つかりました」も役立つと思います。

Rails レンダー ルート パス

sameera207 が述べたように、「ユーザー」をリソースとして宣言することにより、そのリソースに対して定義されたすべての適切なルーティングを取得します。

于 2013-03-28T16:57:27.857 に答える