ruby on rails - undefined method `map' for nil:NilClass when submitting the selection tag -
i new rails. , have question when submitted form. when submitted form, shows
nomethoderror in foods#new, showing ..../new.html.erb line #16 raised: undefined method `map' nil:nilclass while line 16 is:
<%= select_tag(:category_id, options_for_select(@categories), :prompt => "please select") %> i little confused method map, need add create action?
here new action
def new @food = food.new @categories = category.all.map{|c| [ c.name, c.id ] } end and here create action
def create @food = food.new(food_params) @food.category_id = params[:category_id] if @food.save flash[:success] = "adding successful!" redirect_to @food else render 'new' end end def food_params params.require(:food).permit(:name, :price, :category_id, :description, :picture) end would me solve problem?
i see 1 issue in create method.
in event @food.save == false, you're not building
@categories = category.all.map{|c| [ c.name, c.id ] } before
render 'new' so form isn't getting @categories info needs build select statement.
Comments
Post a Comment