没有路由匹配{:controller =>“items”,:action =>“complete”,:list_id => 1,:id => nil}

时间:2013-04-13 07:19:26

标签: ruby-on-rails ruby-on-rails-3 rails-routing

所以localhost有效。当我点击“show”获取列表时,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

1 个答案:

答案 0 :(得分: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>