0
我有Package
belongs_to
Shop
。在我的應用程序中,用戶可以爲他們的商店創建包。我有'創建包'工作,但似乎有一個重定向錯誤,如果我試圖保存沒有輸入任何東西。Rails的form_for重定向嵌套資源錯誤
# 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| %>
任何幫助嗎?
另一個選項是,在錯誤的create action中設置@shop實例變量。 – Swards
我會在出錯時創建'設置@shop實例變量'。 –