1

所以本地主机工作。当我单击列表的“显示”时,URL 会转到 localhost/lists/1 并引发上面的错误。

这是我的 routes.rb 文件:

resources :lists do
  resources :items
end

match 'lists/:list_id/items/:id/complete' => 'items#complete', :as => :complete_item

root :to => 'lists#index'

这是我的 show.html.erb 文件(列表视图):

<p id="notice"><%= notice %></p>

<p>
  <b>Name:</b>
  <%= @list.name %>
</p>

<p>
  <b>Description:</b>
  <%= @list.description %>
</p>

<ul>
    <% @list.items.each do |item| %>
    <li><%= item.name %> <%= button_to "Complete", complete_item_path(@list.id,item.id) %></li>
    <% end %>
</ul>

<%= form_for [@list, @item] do |form| %>
<p><%= form.text_field :name %> <%= form.submit %></p>
<% end %>

<%= link_to 'Edit', edit_list_path(@list) %> |
<%= link_to 'Back', lists_path %>

这是我的 items_controller.rb 文件:

class ItemsController < ApplicationController

    attr_accessor :completed

    respond_to :html, :xml, :js

    def create
        @list = List.find(params[:list_id])
        @item = @list.items.new(params[:item])
        if @item.save
            flash[:notice] = "Item was created."
            redirect_to list_url(@list)
        else
            flash[:error] = "Could not add item, try again later."
            redirect_to list_url(@list)
        end
    end 

    def complete
        @list = List.find(params[:list_id])
        @item = @list.items.find(params[:id])
        @item.completed = true
        @item.save
        redirect_to list_url(@list)
    end

end

这是我在运行 rake 路线时得到的:

    list_items GET    /lists/:list_id/items(.:format)              items#index
               POST   /lists/:list_id/items(.:format)              items#create
 new_list_item GET    /lists/:list_id/items/new(.:format)          items#new
edit_list_item GET    /lists/:list_id/items/:id/edit(.:format)     items#edit
     list_item GET    /lists/:list_id/items/:id(.:format)          items#show
               PUT    /lists/:list_id/items/:id(.:format)          items#update
               DELETE /lists/:list_id/items/:id(.:format)          items#destroy
         lists GET    /lists(.:format)                             lists#index
               POST   /lists(.:format)                             lists#create
      new_list GET    /lists/new(.:format)                         lists#new
     edit_list GET    /lists/:id/edit(.:format)                    lists#edit
          list GET    /lists/:id(.:format)                         lists#show
               PUT    /lists/:id(.:format)                         lists#update
               DELETE /lists/:id(.:format)                         lists#destroy
 complete_item        /lists/:list_id/items/:id/complete(.:format) items#complete
          root        /                                            lists#index

这是我的架构文件:

ActiveRecord::Schema.define(:version => 20130412080044) do

  create_table "items", :force => true do |t|
    t.string   "name"
    t.string   "description"
    t.boolean  "completed",   :default => false
    t.integer  "list_id"
    t.datetime "created_at",                     :null => false
    t.datetime "updated_at",                     :null => false
  end

  add_index "items", ["list_id"], :name => "index_items_on_list_id"

  create_table "lists", :force => true do |t|
    t.string   "name"
    t.string   "description"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end
end

显示 lists_controller.rb 的操作

def show
    @list = List.find(params[:id])
    @item = @list.items.new

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

1 回答 1

2

您可能需要为 complete_item_path 方法使用参数哈希:

complete_item_path( :list_id => @list.id, :id => item.id)

第二件事是@list.items 集合中有一个未保存的项目。生成链接时,您需要忽略它:

<ul>
  <% @list.items.each do |item| %>
    <% if item.id %>
      <li><%= item.name %> <%= button_to "Complete", complete_item_path(@list.id,item.id) %></li>
    <% end %>
  <% end %>
</ul>
于 2013-04-13T07:26:07.150 回答