最初の Rails アプリでは、ユーザー サインイン用のページ (sessions new) とユーザー サインアップ用のページ (users new) がありましたが、2 番目の 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
------------- 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
------------ ビュー/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>
前もって感謝します!