I've got a working form that properly saves all attributes including a collection_select
. However, when I add validates :title, presence: true
and try to submit, I get
undefined method 'map' for nil:NilClass
My new action:
class ItemsController < ApplicationController
def new
@item = Item.new
@categories = Question.editable_by(current_user)
respond_to do |format|
format.html { render :layout => true}
format.json { render :json => @item }
format.js
end
end
end
View is items\_form.html.haml
Does not work
= f.collection_select :question_id, @categories, :_id, :iqs_item
Works
= f.collection_select :question_id, Question.editable_by(current_user), :_id, :iqs_item
This works when I add the @categories
object from the controller to the view. I'm guessing something doesn't get passed for the error
function in the view or it can't map the select when it tries to redraw the page with errors??
Please assist. Again, I can get it to work, but I'd rather not be putting method calls in the view.
= f.collection_select :question_id, @categories, :_id, :iqs_item
line would give undefined method 'map' for nil:NilClass
error when collection given to it ,i.e., @categories
is set as nil
.
Add below mentioned code in the view, if your page displays successfully(without error message) and without a select
then you know that @categories was set as nil.
- unless @categories.nil?
= f.collection_select :question_id, @categories, :_id, :iqs_item
EDIT
Also set @categories
in the create method