Rails 3.2アプリで、特定のフォームの保存に失敗し(検証が失敗し)、フォームにリダイレクトすると、エラーが発生します。
undefined method `map' for nil:NilClass
このフォームは、新しいパスまたは編集パスに直接移動するときにエラーを表示しません。
options_from_collection_for_select
エラーは、カスタムメソッドを使用した選択フィールドから発生しています。
<%= f.select(:user_ids, options_from_collection_for_select_with_attributes(@users, :id, :name, 'data-attributes', :attributes ), {include_blank:true}, {multiple:true}) %>
@users
インスタンス変数をに置き換えても、User.all
リダイレクト後にエラーは発生しません。
@users
リダイレクト後は空なので、エラーだと思います。しかし、なぜ?@users
newおよびeditコントローラーで定義されます。
私のコントローラーは:
def create
--bunch of stuff
if @model.save
--bunch of stuff
respond_to do |format|
format.html { render :text => model_url(@model) }
format.html { redirect_to(@model, :notice => 'Success!.') }
format.xml { render :xml => @model, :status => :created, :location => @model }
end
else
respond_to do |format|
format.html { render :action => "new" }
format.xml { render :xml => @model.errors, :status => :unprocessable_entity }
end
end
end