Couldn't find UserSession without an ID in ruby on rails 3
ActiveRecord::RecordNotFound in UsersController#index
Couldn't find UserSession without an ID Rails.root: C:/Sites/login_app
Application Trace | Framework Trace | Full Trace app/controllers/application_controller.rb:9:in current_user_session' app/controllers/application_controller.rb:14:in
current_user' app/views/layouts/application.html.erb:12:in _app_views_layouts_application_html_erb__233847886_33215772' app/controllers/users_controller.rb:7:in
index'
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
end
class HomeController < ApplicationController
def index
end
end
class UserSessionsController < ApplicationController
def new
@user_session = UserSession.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user_session }
end
end
def create
@user_session = UserSession.new(params[:user_session])
respond_to do |format|
if @user_session.save
format.html { redirect_to(:users, :notice => 'Login Successful') }
format.xml { render :xml => @user_session, :status => :created, :location => @user_session }
else
format.html { render :action => "new" }
format.xml { render :xml => @user_session.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@user_session = UserSession.find
@user_session.destroy
respond_to do |format|
format.html { redirect_to(:users, :notice => 'Goodbye!') }
format.xml { head :ok }
end
end
end
class UsersController < ApplicationController
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @users }
end
end
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @user }
end
end
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user }
end
end
def edit
@user = User.find(params[:id])
end
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to(:users, :notice => 'Registration successfull.') }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
end
layout/apllication.html
LoginApp
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
<% if current_user %>
<%= link_to "Edit Profile", edit_user_path(current_user.id)%>
<%= link_to "Logout", :logout%>
<% else %>
<%= link_to "Register", new_user_path%>
<%= link_to "Login", :login %>
<% end %>
<%= yield %>
user_seesion/_form.html
<%= form_for(@user_session) do |f| %>
<% if @user_session.errors.any? %>
<%= pluralize(@user_session.errors.count, "error") %> prohibited this user_session from being saved:
<% @user_session.errors.full_messages.each do |msg| %>
<% end %>
<% end %>
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.label :password %>
<%= f.password_fields :password %>
<%= f.submit %>
<% end %>
user_sessions/edit.html
Editing user_session
<%= render 'form' %>
<%= link_to 'Show', @user_session %>
| <%= link_to 'Back', user_sessions_path %>
user_sessions/index.html :
Listing user_sessions
Username
Password
<% @user_sessions.each do |user_session| %>
<%= user_session.username %>
<%= user_session.password %>
<%= link_to 'Show', user_session %>
<%= link_to 'Edit', edit_user_session_path(user_session) %>
<%= link_to 'Destroy', user_session, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
<%= link_to 'New User session', new_user_session_path %>
Login
<%= render 'form' %>
<%= link_to 'Back', user_sessions_path %>
<%= notice %>
Username:
<%= @user_session.username %>
Password:
<%= @user_session.password %>
<%= link_to 'Edit', edit_user_session_path(@user_session) %>
| <%= link_to 'Back', user_sessions_path %>
-users/_form.html
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:
<ul><br/>
<% @user.errors.full_messages.each do |msg| %><br/>
<li><%= msg %></li><br/>
<% end %><br/>
</ul><br/>
</div><br/>
<% end %>
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<%= f.submit 'create user'%>
<% end %>
Editing user
<%= render 'form' %>
<%= link_to 'Show', @user %>
| <%= link_to 'Back', users_path %>
Listing users
<%= notice %>
Username
<% @users.each do |user| %>
<%= user.username %>
<%= user.email %>
<%= link_to 'Show', user %>
<%= link_to 'Edit', edit_user_path(user) %>
<%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
<%= link_to 'New User', new_user_path %>
New user
<%= render 'form' %>
<%= link_to 'Back', users_path %>
<%= notice %>
Username:
<%= @user.username %>
Email:
<%= @user.email %>
Crypted password:
<%= @user.crypted_password %>
Password salt:
<%= @user.password_salt %>
Persistence token:
<%= @user.persistence_token %>
<%= link_to 'Edit', edit_user_path(@user) %>
| <%= link_to 'Back', users_path %>
In user_session.rb, you need to change the inherited class to Authlogic::Session::Base, like so:
class UserSession < Authlogic::Session::Base
...
end
Also, next time use a gist or something similar to paste large chunks of code, or format it at least. Hope that helps!
链接地址: http://www.djcxy.com/p/35958.html上一篇: 悲剧和行为