我有Package
belongs_to
Shop
。在我的应用程序中,用户可以为他们的商店创建包。我已经让“创建包”工作,但如果我尝试保存而不输入任何内容,重定向似乎会出错。
# package.rb
belongs_to :shop
validates_presence_of :name, :price, :description
# controller
def new
@shop = current_user.shop
@package = Package.new
end
def create
@package = current_user.shop.packages.build(params[:package])
@package.currency = current_user.shop.currency
if @package.save
flash[:success] = 'Package added.'
redirect_to shop_path(current_user.shop)
else
render 'new'
end
end
# routes.rb
resources :shops, except: [:destroy] do
resources :packages
end
# new.html.erb
<%= form_for [@shop, @package] do |f| %>
这是错误:
NoMethodError in Packages#create
undefined method `packages_path' for #<#<Class:0x007fab67191580>:0x007fab634ad7b8>
Extracted source (around line #6):
5:
6: <%= form_for [@shop, @package] do |f| %>
有什么帮助吗?