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
4

1 回答 1

1

您在路线文件中缺少update操作。items

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

应该

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

或者,更简洁地说,

resources :items, except: [:show]
于 2013-06-29T21:11:40.673 回答