0

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

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

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

NilClass:Class 的未定义方法“model_name”

有人有想法么?相关代码(仅)如下。

------------- 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

------------- session_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

------------ 意见/static_pages/home.html.erb ----------------

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

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

------------- 视图/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>

------------- 视图/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>

------------- 意见/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>

提前致谢!

4

1 回答 1

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注册失败时使用视图。

于 2013-07-16T02:44:21.410 回答