我正在研究一個rails任務,要求我創建一個項目列表。我爲物品創建了控制器和模型,但仍然遇到一些麻煩。我不斷收到以下錯誤:如何爲項目創建新/創建操作?
undefined method `items_path' for
下面是我的一些代碼:
class ItemsController < ApplicationController
def new
@item = Item.new
end
def create
@item = Item.new(params.require(:item).permit(:name))
if @item.save
flash[:notice] = "Item was saved."
redirect_to @item
else
flash[:error] = "There was an error saving the item. Please try again."
render :new
end
end
end
產品型號:
class Item < ActiveRecord::Base
belongs_to :user
end
New.html.erb在項目
class Item < ActiveRecord::Base
belongs_to :user
end
Routes.rb:
Rails.application.routes.draw do
devise_for :users
resources :users do
resources :items, only: [:new, :create]
end
get 'welcome/index'
root :to => 'welcome#index'
end
Item.html.erb
<%= form_for @item do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit "Save" %>
<% end %>
您正在使用'@的form_for item'在你的意見中,是嗎?添加相關代碼可能會改善問題,但問題似乎很明顯。 –
Yup剛剛添加了視圖,對此感到抱歉 –
http://stackoverflow.com/questions/2034700/form-for-with-nested-resources –