Полный текст сообщения об ошибке:ActiveRecord :: RecordNotFound в TodosController # индекс
ActiveRecord::RecordNotFound in TodosController#index
Couldn't find User with id=2
Rails.root: /home/randy/rubystack-1.9.3-29/projects/chap14
app/controllers/application_controller.rb:11:in current_user
app/views/todos/index.html.erb:21:in _app_views_todos_index_html_erb___949818655437808348_39324440
app/controllers/todos_controller.rb:8:in index
Вот код для моего application_controller.rb:
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."
А вот мой todos_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/index.html. У меня установлен мой корневой маршрут. Поблагодарили бы за помощь.
Пожалуйста, включите представление 'index.html.erb'. – kalelc
Включено представление index.html.erb из каталога todos. Спасибо, kalelc –
Хорошо, в строке 'if current_user.admin?'. 'current_user' (предположим, что это модель пользователя) есть поле для проверки? – kalelc