尝试查看列表中包含的项目时出现以下错误。
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
show.html.erb是
<div class="container">
<h1><%= @list.name %></h1>
<div class="row">
<h2>Items</h2>
<div class="col-md-8">
<div class="js-items">
<%= render :partial => 'items/item' %>
</div>
<div class="new-item">
<%= render :partial => 'items/form' %>
</div>
</div>
<div class="row">
<div class="col-md-8">
</div>
</div>
</div>
部分:
表格
<% if current_user %>
<h4>Add an item:</h4>
<%= form_for [@list, @list.items.build], remote: true do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_area :name, class: 'form-control', placeholder: "Enter item details" %>
<br>
<%= f.submit "Add Item", class: 'btn btn-primary' %>
</div>
<% end %>
<% end %>
项目
<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 %>
现在我收到了类似的错误,现在只在我的Items控件中
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