TodosController #index中的ActiveRecord :: RecordNotFound

时间:2015-11-18 16:21:07

标签: ruby-on-rails activerecord

错误消息的全文是:

class ApplicationController < ActionController::Base
  protect_from_forgery

   helper_method :current_user


      private

      def current_user
        if session[:user_id]
          @current_user ||= User.find(session[:user_id])
        else
          @current_user = nil
        end
      end

      def check_login
        unless authorized?
          redirect_to "/auth/identity"
        end
      end


      def logged_in?
        if session[:user_id]
          return true
        else
          return false
        end
      end

      protected
       def authorized?
          logged_in? && (request.get? || current_user.admin?)
       end    

end."

这是我的application_controller.rb的代码:

class TodosController < ApplicationController
  before_filter :check_login
  # GET /todos
  # GET /todos.json
  def index
    @todos = Todo.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @todos }
    end
  end

  # GET /todos/1
  # GET /todos/1.json
  def show
    @todo = Todo.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @todo }
    end
  end

  # GET /todos/new
  # GET /todos/new.json
  def new
    @todo = Todo.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @todo }
    end
  end

  # GET /todos/1/edit
  def edit
    @todo = Todo.find(params[:id])
  end

  # POST /todos
  # POST /todos.json
  def create
    @todo = Todo.new(params[:todo])

    respond_to do |format|
      if @todo.save
        format.html { redirect_to @todo, notice: 'Todo was successfully created.' }
        format.json { render json: @todo, status: :created, location: @todo }
      else
        format.html { render action: "new" }
        format.json { render json: @todo.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /todos/1
  # PUT /todos/1.json
  def update
    @todo = Todo.find(params[:id])

    respond_to do |format|
      if @todo.update_attributes(params[:todo])
        format.html { redirect_to @todo, notice: 'Todo was successfully updated.' }
        format.json { head :no_content }

        @todo = Todo.find(params[:id])
        if @todo.completed == true
          @todo.user_who_completed = current_user.email
          @todo.save
        end
      else
        format.html { render action: "edit" }
        format.json { render json: @todo.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /todos/1
  # DELETE /todos/1.json
  def destroy
    @todo = Todo.find(params[:id])
    @todo.destroy

    respond_to do |format|
      format.html { redirect_to todos_url }
      format.json { head :no_content }
    end
  end
end."

And my app/views/todos/index.html.erb file:

"<h1>Listing todos</h1>

<table>
  <tr>
    <th>Name</th>
    <th>Completed</th>
    <th>Completed date</th>
    <th>User</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% for todo in @todos %>
  <tr>
    <td><%= todo.name %></td>
    <td><%= todo.completed %></td>
    <td><%= todo.completed_date %></td>
    <td><%= todo.user %></td>
    <% end %>
    <% if current_user.admin? %>
    <td><%= link_to 'Show', todo %></td>
    <td><%= link_to 'Edit', edit_todo_path(todo) %></td>
    <td><%= link_to 'Destroy', todo, method: :delete, data: { confirm: 'Are you sure?' } %></td>S
  </tr>
<% end %>
</table>

<br />
<% if current_user.admin? %>
<%= link_to 'New Todo', new_todo_path %>
<% end %>

这是我的todos_controller.rb:

.git

我是rails的新手,对错误信息的含义及其原因一无所知。我希望访问者登陆todos / index.html页面。我有相应的根路由设置。希望得到一些帮助。

2 个答案:

答案 0 :(得分:0)

该错误来自User.find。它在Documentation for find中描述。

它告诉你没有User具有该ID。不知何故,您已将用户ID保存到会话中,该用户ID对应于当前不存在的用户。

您应该检查如何将用户ID存储到会话中以及在什么情况下可以删除用户。

答案 1 :(得分:0)

问题在于你的循环。如果有条件,它就在循环之外。

检查解决方案视图:

<h1>Listing todos</h1>

<table>
  <tr>
    <th>Name</th>
    <th>Completed</th>
    <th>Completed date</th>
    <th>User</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% for todo in @todos %>
  <tr>
    <td><%= todo.name %></td>
    <td><%= todo.completed %></td>
    <td><%= todo.completed_date %></td>
    <td><%= todo.user %></td>
    <% if current_user.admin? %>
      <td><%= link_to 'Show', todo %></td>
      <td><%= link_to 'Edit', edit_todo_path(todo) %></td>
      <td><%= link_to 'Destroy', todo, method: :delete, data: { confirm: 'Are you sure?' } %></td>
    <% end %>
  </tr>
<% end %>
</table>

建议

  • 检查您的问题。您拥有带控制器代码的代码视图。
  • 小心缩进。