在此Rails應用程序中,用戶可以編寫故事並將其添加到集合。在他們編寫故事時,用戶可以將其添加到現有的集合中,或者通過模式在story/new.html.erb視圖中創建一個新的集合。更改爲嵌套資源後表格被破壞
它看起來像這樣目前
的routes.rb
resources :users
resources :collections
new.html.erb
<%= form_for Collection.new do |f| %>
收藏控制器
class CollectionsController < ApplicationController
def new
@user = current_user # or however
@collection = Collection.new
end
def show
@collection = Collection.friendly.find(params[:id])
end
def create
@collection = current_user.collections.build(collection_params)
if @collection.save
render json: @collection
else
render json: {errors: @collection.errors.full_messages}
end
end
private
def collection_params
params.require(:collection).permit(:name, :description)
end
end
故事控制器
class StoriesController < ApplicationController
def new
@story = Story.new
authorize @story
end
end
現在我要嵌套的路線,使得藏品屬於用戶本身
resources :users do
resources :collections
然而,導致在這條線
<%= form_for Collection.new do |f| %>
,它不再起作用錯誤。如何解決這個問題?謝謝。
感謝您的支持。我仍然收到此錯誤「表單中的第一個參數不能包含零或爲空」,用於行<%= form_for [@user,@collection] do | f | %> – Joshua
您需要確保'@ user'和'@ collection'都已定義 – fbelanger
在這種情況下,我應該在哪裏定義它們?就我所知,我已經在控制器中這樣做了。謝謝。 – Joshua