Rails 4注册表单未显示确认错误

时间:2014-11-20 20:50:57

标签: ruby-on-rails

我是rails world的新手,需要有关我的应用注册表单的帮助。点击"提交我的帐户"确认字段未显示错误带有空白表格的按钮。

我的 user.rb

class User < ActiveRecord::Base
 before_save { self.email = email.downcase}
 validates :name,  presence: true, length: { minimum: 2, maximum: 50 }
 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
 validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
       uniqueness: { case_sensitive: false}
 validates :password, length: { minimum: 6 }
 has_secure_password
 has_attached_file :avatar,
           :styles => { :medium => "300x300>", :thumb => "100x100>" },
           :default_url => "/images/:style/missing.png"
 validates_attachment_content_type :avatar, :content_type => /\Aimage/
 validates_attachment_file_name :avatar, :matches => [/png\Z/, /jpe?g\Z/]
 do_not_validate_attachment_file_type :avatar
 validates_attachment_size :avatar, :less_than => 2.megabytes
end

**users_controller.rb**


   class UsersController < ApplicationController
  def new
   @user = User.new
  end

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

  def create
   @user = User.new(user_params)
   if @user.save
    redirect_to @user
    flash[:success] = 'Welcome to the Survey App'
   else
    render 'new'
   end
  end

  private
   def user_params
    params.require(:user).permit(:name, :email, :password,
                     :password_confirmation, :avatar)
   end
end

**_error_messages.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 %>

**routes.rb**
SurveyApp::Application.routes.draw do

  get "users/new"
  mount Rapidfire::Engine => "/rapidfire"

  resources :users
  match '/signup', to: 'users#new', via: 'get'
end

**new.html.erb**
    <% provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class ="row">
 <div class ="span6 offset3">
  <%= form_for @user, :url => users_path,
               :html => { :multipart => true } 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 %>

  <p> <%= f.label :avatar, "Your avatar" %>
      <%= f.file_field :avatar %> </p>


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

  <% end %>
 </div>
</div>

**Gemfile:**
source 'https://rubygems.org'
ruby '2.0.0'

gem 'rails', '4.0.3'
gem 'bootstrap-sass', '2.3.2.0'

group :test do
 gem 'selenium-webdriver', '2.35.1'
 gem 'capybara', '2.1.0'
 gem 'factory_girl_rails', '4.2.1'

end

gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'


# Use SCSS for stylesheets
gem 'sass-rails', '4.0.1'

gem 'sprockets', '=2.11.0'

gem 'uglifier', '>= 1.3.0'

gem 'coffee-rails', '~> 4.0.0'

gem 'jquery-rails'


gem 'turbolinks', '1.1.1'


gem 'jbuilder', '~> 1.2'

group :doc do

  gem 'sdoc', require: false
end

gem 'rapidfire'

gem "paperclip", "~> 4.2"

gem 'bcrypt-ruby', '3.1.2'

任何帮助表示赞赏! Tnx:)

0 个答案:

没有答案