-2

ActiveRecord::RecordNotFound in UsersController#index

找不到没有 ID Rails.root 的 UserSession:C:/Sites/login_app

应用程序跟踪 | 框架跟踪 | 完整跟踪 app/controllers/application_controller.rb:9:in current_user_session' app/controllers/application_controller.rb:14:incurrent_user' app/views/layouts/application.html.erb:12:in _app_views_layouts_application_html_erb__233847886_33215772' app/controllers/users_controller.rb:7:inindex'

  • class ApplicationController < ActionController::Base
    protect_from_forgery
    helper_method :current_user private
    def current_user_session
    如果定义返回@current_user_session?(@current_user_session)
    @current_user_session = UserSession.find
    end
    def current_user
    如果定义返回@current_user?(@current_user)
    @current_user = current_user_session && current_user_session。记录
    结束
    结束

  • 类 HomeController < ApplicationController
    def index
    end
    end

  • 类用户会话控制器 < 应用控制器

    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 => '登录成功') }
    format.xml { 渲染 :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 做 |格式|
    format.html { redirect_to(:users, :notice => '再见!') }
    格式。
    结束
    结束
    结束

  • 类用户控制器 < 应用控制器

    def index
    @users = User.all
    respond_to do |format|
    format.html # index.html.erb
    format.json { 渲染 json: @users }
    end
    end

    def show
    @user = User.find(params[:id])
    respond_to do |format|
    format.html # show.html.erb
    format.json { 渲染 json: @user }
    end
    end

    def new
    @user = User.new
    respond_to do |format|
    format.html # new.html.erb
    format.json { 渲染 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 => '注册成功') }
    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: '用户已成功更新。' }
format.json { head :no_content }
else format.html
{ 渲染动作:“编辑” }
format.json { 渲染 json:@user.errors,状态::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%>
    <% else %>
    <%= link_to "注册", new_user_path%>
    <%= link_to "登录", :login %>
    <% end %>

    <%= yield %>


  • user_seesion/_form.html
    <%= form_for(@user_session) 做 |f| %>
    <% if @user_session.errors.any? %>

    <%= 复数(@user_session.errors.count, "error") %> 禁止保存此 user_session:



      <% @user_session.errors.full_messages.each 做 |msg| %>
    • <%= 消息 %>

    • <% 结束 %>


    <% 结束 %>


    <%= f.label :username %>

    <%= f.text_field :username %>


    <%= f.label :password %>

    <%= f.password_fields :password %>


    <%= f.submit %>

    <%结束%>

  • user_sessions/edit.html

    编辑 user_session


    <%= render 'form' %>
    <%= link_to 'Show', @user_session %>
    | <%= link_to '返回', user_sessions_path %>

  • user_sessions/index.html :

    列出 user_sessions




用户名
密码





<% @user_sessions.each 做 |user_session| %>

<%= user_session.username %>
<%= user_session.password %>
<%= link_to '显示',user_session %>
<%= link_to '编辑',edit_user_session_path(user_session) %>
<%= link_to '销毁' , user_session, method: :delete, data: { confirm: 'Are you sure?' } %>

<% 结束 %>


<%= link_to '新用户会话', new_user_session_path %>

  • user_sessions/new.html

    登录



<%= 渲染'表格' %>

<%= link_to '返回', user_sessions_path %>

  • user_sessions/show.html

    <%= 通知 %>




    用户名:
    <%= @user_session.username %>



密码:
<%= @user_session.password %>


<%= link_to '编辑', edit_user_session_path(@user_session) %>
| <%= link_to '返回', user_sessions_path %>

-users/_form.html
<%= form_for(@user) 做 |f| %>
<% 如果@user.errors.any? %>

<%= 复数(@user.errors.count, "error") %> 禁止保存该用户:


  <ul><br/>
  <% @user.errors.full_messages.each do |msg| %><br/>
    <li><%= msg %></li><br/>
  <% end %><br/>
  </ul><br/>
</div><br/>

<% 结束 %>


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

  • 用户/edit.html

    编辑用户


<%= 渲染'表格' %>

<%= link_to '显示', @user %>
| <%= link_to '返回', users_path %>

  • 用户/index.html :

    列出用户


    <%= 通知 %>




    用户名
    电子邮件




<% @users.each 做 |user| %>

<%= user.username %>
<%= user.email %>
<%= link_to '显示',用户 %>
<%= link_to '编辑',edit_user_path(user) %>
<%= link_to '销毁' , user, :confirm => 'Are you sure?', :method => :delete %>

<% end %>


<%= link_to '新用户', new_user_path %>

  • 用户/new.html :

    新用户


<%= render 'form' %>
<%= link_to 'Back', users_path %>

  • users/show.html :
    <%= 通知 %>



用户名:
<%= @user.username %>



电子邮件:
<%= @user.email %>



加密密码:
<%= @user.crypted_pa​​ssword %>



密码盐:
<%= @user.password_salt %>



持久性令牌:
<%= @user.persistence_token %>


<%= link_to '编辑', edit_user_path(@user) %>
| <%= link_to '返回', users_path %>

4

1 回答 1

1

在 user_session.rb 中,需要将继承的类改为 Authlogic::Session::Base,如下所示:

class UserSession < Authlogic::Session::Base
...
end

此外,下次使用 gist 或类似的东西来粘贴大块代码,或至少对其进行格式化。希望有帮助!

于 2013-02-07T13:52:59.473 回答