登录/注册在主页上时,验证错误会出错

时间:2013-07-16 02:32:17

标签: ruby-on-rails ruby-on-rails-3

在我的第一个Rails应用程序中,我有一个用户登录页面(会话新)和一个用户注册页面(用户新用户),但在我的第二个Rails应用程序中,我想要在主页上登录和注册。

当用户输入正确的信息时,我终于得到了这个功能,例如实际存在的用户电子邮件和正确的密码。

但是如果用户输入错误的信息,例如不存在的注册用户电子邮件或密码错误,我会收到错误消息:

NilClass的未定义方法'model_name':Class

有人有什么想法吗?相关代码(仅限)。

------------- routes.rb ----------------

MyApp::Application.routes.draw do
  resources                 :sessions, only: [:create, :destroy]
  resources                 :users

  root                      to: 'static_pages#home'
end

-------------- user.rb ----------------

class User < ActiveRecord::Base
  attr_accessible :email, :email_confirmation, :firstname, :lastname, :password, :password_confirmation,

  validates :email, presence: true
  validates :firstname, presence: true
  validates :lastname, presence: true
  validates :password, length: { within: 6..16 }, if: :password
  validates :password_confirmation, presence: true, if: :password
end

-------- users_controller.rb --------------

class UsersController < ApplicationController
  def new
    if signed_in?
      redirect_to root_path
    else
      @user = User.new
    end
  end

  def create
    if signed_in?
      redirect_to root_path
    else
      @user = User.new(params[:user])
      if @user.save
        sign_in @user
        flash[:success] = "Welcome #{@user.firstname} to MyApp. Thanks for signing up!"
        redirect_to root_path
      else
        render 'new'
      end
    end
  end
end

------------- sessions_controller.rb ----------------

class SessionsController < ApplicationController
  def create
    user = User.find_by_email(params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      sign_in user
      redirect_back_or user_path(current_user)
    else
      flash.now[:error] = "Invalid email/password combination"
      user = User.create(email: params[:session][:email], password: params[:session][:password])
      render 'static_pages/home', locals: { showerror: true, user: user }
    end
  end
end

------- static_pages_controller.rb --------------

class StaticPagesController < ApplicationController
  def home 
    @user = User.new
  end
end

----------- application_helper.rb -------------

module ApplicationHelper
  def field_class(resource, field_name)
    if resource.errors[field_name].length > 0
      return " custom_error1 ".html_safe
    else
      return "".html_safe
    end
  end  

  def error_notifier(error)
    if error
      return " custom_error1 ".html_safe
    else
      return "".html_safe
    end
  end    
end

------------ views / static_pages / home.html.erb ----------------

<div>
  <div>
    <!-- signin form -->
    <%= render 'static_pages/new_session' %>

    <!-- signup form -->
    <%= render 'static_pages/new_user' %>
  </div>
</div>

------------- views / static_pages / _new_session.html.erb ---------------

<div>
  <h3>Sign in</h3>
  <%= form_tag(sessions_path) do %>
    <div>
      <div>
        <%= label :session, :email %>
        <%= text_field :session, :email, class: (error_notifier(@showerror) + 'info_inline_control info_textfield') %>
      </div>
      <div>
        <%= label :session, :password %>
        <%= password_field :session, :password, class: (error_notifier(@showerror) + 'info_inline_control info_textfield') %>
      </div>
    </div>
    <div class="signin_link">
      <%= submit_tag "Sign in" %>

      <p class="reminders">Forgot your password? 
        <%= link_to "Reset password", new_password_reset_path %></p>
    </div>
  <% end %>
</div>

------------- views / static_pages / _new_user.html.erb ---------------

<div>
  <h3>Sign up</h3>  
  <%= form_for(@user) do |f| %>
    <%= render 'user_fields', f: f %>
    <div>
      <%= f.submit "Create Account" %> 
    </div>
  <% end %>
</div>

------------- views / static_pages / _user_fields.html.erb ---------------

<%= render 'shared/error_messages', object: f.object %>

<div>
  <div>
    <%= f.label :firstname %>
    <%= f.text_field :firstname, class: (field_class(@user, :firstname) + 'info_inline_control info_textfield') %>
  </div>
  <div>
    <%= f.label :lastname %>
    <%= f.text_field :lastname, class: (field_class(@user, :lastname) + 'info_inline_control info_textfield') %>
  </div>
  <div>
    <%= f.label :email %>
    <%= f.text_field :email, class: (field_class(@user, :email) + 'info_inline_control info_textfield') %>
  </div>
  <div>
    <%= f.label :password %>
    <%= f.password_field :password, class: (field_class(@user, :password) + 'info_inline_control info_textfield') %>
  </div>
  <div>
    <%= f.label :password_confirmation, "Confirm Password" %>
    <%= f.password_field :password_confirmation, class: (field_class(@user, :password_confirmation) + 'info_inline_control info_textfield') %>
  </div>
</div>

提前致谢!

1 个答案:

答案 0 :(得分:0)

class SessionsController < ApplicationController
  def create
    @user = User.find_by_email(params[:session][:email].downcase)
    if @user && @user.authenticate(params[:session][:password])
      sign_in @user
      redirect_back_or user_path(current_user)
    else
      flash.now[:error] = "Invalid email/password combination"
      @user = User.create(email: params[:session][:email], password: params[:session][:password])
      @showerror = true
      render 'static_pages/home'
    end
  end
end

您应该使用实例变量@user,因为在注册失败时使用视图@user