2013-06-29 40 views
0

我有一個項目模型,我不需要單獨的顯示視圖。相反,當項目更新時,我想將用戶返回到索引。當表單提交給編輯項目,你會得到這樣的錯誤:No route matches [PUT] "/items/1"沒有顯示視圖的更新操作

這裏是路線文件

Order::Application.routes.draw do 

    root to: 'static_pages#home' 

    resources :static_pages 
    resources :customers 
    resources :demands 

    resources :items, only: [:new, :create, :destroy, :index, :edit] 


end 

這裏是控制器

class ItemsController < ApplicationController 

    def index 
     @items = Item.all 
    end 

    def new 
     @item = Item.new 
    end 

    def create 
     @item = Item.new(params[:item]) 
     if @item.save 
      flash[:success] = "Item saved!" 
      redirect_to items_path 
     else 
      render new_item_path 
     end 
    end 

    def destroy 
     Item.find(params[:id]).destroy 
     redirect_to items_path 
    end 

    def edit 
     @item = Item.find(params[:id]) 
    end 

    def update 
     @item = Item.find(params[:id]) 
     if @item.update_attributes(params[:item]) 
      redirect_to 'items#index' 
      flash[:success] = "Item updated!" 
     else 
      render 'edit' 
     end 
    end 


end 

這裏是模型

class Item < ActiveRecord::Base 
    attr_accessible :name, :price 

    validates :name, presence: true 

    VALID_PRICE_REGEX = /^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}$/ 
    validates :price, presence: true, format: {with: VALID_PRICE_REGEX} 

end 

回答

1

您錯過了update針對的操作在你的路線文件中。

resources :items, only: [:new, :create, :destroy, :index, :edit] 

應該是

resources :items, only: [:new, :create, :destroy, :index, :edit, :update] 

或,更簡明地,

resources :items, except: [:show] 
+0

稀釋當然。我不能相信我錯過了那麼簡單的事情。謝謝! – Michael