我有一个Item模型,我不需要单独的show view。相反,当项目更新时,我想将用户返回到索引。提交表单以编辑项目时,会出现如下错误: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
答案 0 :(得分:1)
您错过路线文件中update
的{{1}}操作。
items
应该是
resources :items, only: [:new, :create, :destroy, :index, :edit]
或者更简洁地说,
resources :items, only: [:new, :create, :destroy, :index, :edit, :update]