Ruby on rails ActiveRecord::在ListsController#show中找不到RecordNot
试图查看列表中包含的项目时,我遇到以下错误Ruby on rails ActiveRecord::在ListsController#show中找不到RecordNot,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,试图查看列表中包含的项目时,我遇到以下错误 Started GET "/lists/item" for ::1 at 2015-06-29 21:17:55 -0600 Processing by ListsController#show as HTML Parameters: {"id"=>"item"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER
Started GET "/lists/item" for ::1 at 2015-06-29 21:17:55 -0600
Processing by ListsController#show as HTML
Parameters: {"id"=>"item"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 3]]
List Load (0.1ms) SELECT "lists".* FROM "lists" WHERE "lists"."user_id" = ? AND "lists"."id" = ? LIMIT 1 [["user_id", 3], ["id", 0]]
Completed 404 Not Found in 5ms (ActiveRecord: 0.3ms)
ActiveRecord::RecordNotFound (Couldn't find List with 'id'=item [WHERE "lists"."user_id" = ?]):
app/controllers/lists_controller.rb:44:in `set_list'
这是我的控制器:
class ListsController < ApplicationController
before_action :set_list
def new
@list = current_user.lists.new
end
def create
@list = current_user.lists.build(list_params)
if @list.save
flash[:notice] = "Your list was created successfully"
redirect_to user_path(current_user)
else
flash[:error] = "There was an error saving your new list, please try again."
render :new
end
end
def show
@list = current_user.lists.find(params[:id])
@items = @list.items
end
def edit
end
def update
if @list.update_attributes(list_params)
redirect_to user_path(current_user)
else
flash[:error] = "There was an error saving your list, please try again."
render :edit
end
end
def destroy
@list.destroy
end
private
def set_list
@list = current_user.lists.find(params[:id])
end
def list_params
params.require(:list).permit(:name, :description)
end
end
项目控制员
ActiveRecord::RecordNotFound (Couldn't find List with 'id'=3 [WHERE "lists"."user_id" = ?]):
app/controllers/items_controller.rb:48:in `find_list'
class ItemsController < ActionController::Base
before_action :find_list
def new
@item = @list.items.new
end
def show
end
def create
@new_item = Item.new
@item = @list.items.new(item_params)
if @item.save
flash[:notice] = "Item added successfully"
else
flash[:error] = "There was an error adding your item to the list, please try again."
end
respond_to do |format|
format.html
format.js
end
end
def destroy
@item = @list.items.find(params[:id])
if @item.destroy
flash[:notice]= "#{@item.name} has been completed. Good Job!"
else
flash[:error]= "Sorry looks like there was a problem marking your item as complete, please try again."
end
respond_to do |format|
format.html
format.js
end
end
private
def find_list
@list = current_user.lists.find(params[:list_id])
end
def item_params
params.require(:item).permit(:name)
end
end
class ItemsController
出现此错误时(可能在索引中),您正在单击哪个链接?错误是因为您的路径是/lists/item
。由于显示路线需要列表/:id
,因此假定id
是项
。我正在单击单个列表的链接“
应该做这个恶作剧@AbM我已经做出了改变,但现在我的物品控制器也出现了类似的错误。对rails来说是相当陌生的,并且试图弄清楚为什么我一直有这些路由问题。@lists
是在哪里定义的?您确定id为3的列表属于当前用户吗?
<div class="media-body">
<% @list.items.each do |item| %>
<div id="<%= item.id %>">
<div class="row">
<div class="col-md-4">
<%= item.name %>
</div>
<div class ="col-md-6">
<%= time_ago_in_words(item.created_at) %> ago (Days remaining: <%= item.days_left %> )
</div>
<%= link_to "", list_item_path(item.list, item), class: 'glyphicon glyphicon-ok', method: :delete, remote: true %>
</div>
<% end %>
<hr>
</div>
<% @lists.each do |list| %>
<li><%=link_to list.name , list_path(current_user, list), class: 'btn btn-default btn-lg btn-block' %></li>
<% end %>
ActiveRecord::RecordNotFound (Couldn't find List with 'id'=3 [WHERE "lists"."user_id" = ?]):
app/controllers/items_controller.rb:48:in `find_list'
class ItemsController < ActionController::Base
before_action :find_list
def new
@item = @list.items.new
end
def show
end
def create
@new_item = Item.new
@item = @list.items.new(item_params)
if @item.save
flash[:notice] = "Item added successfully"
else
flash[:error] = "There was an error adding your item to the list, please try again."
end
respond_to do |format|
format.html
format.js
end
end
def destroy
@item = @list.items.find(params[:id])
if @item.destroy
flash[:notice]= "#{@item.name} has been completed. Good Job!"
else
flash[:error]= "Sorry looks like there was a problem marking your item as complete, please try again."
end
respond_to do |format|
format.html
format.js
end
end
private
def find_list
@list = current_user.lists.find(params[:list_id])
end
def item_params
params.require(:item).permit(:name)
end
end