我有一个工作表单,正确保存所有属性,包括collection_select
。然而,当我添加validates :title, presence: true
并尝试提交,我得到Rails奇怪的错误时提交collection_select w /无关验证
undefined method 'map' for nil:NilClass
我的新动作:
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是items\_form.html.haml
不工作
= f.collection_select :question_id, @categories, :_id, :iqs_item
作品
= f.collection_select :question_id, Question.editable_by(current_user), :_id, :iqs_item
这工作时我从控制器到视图添加@categories
对象。我猜在视图中error
函数没有通过,或者当它试图重绘错误页面时它不能映射选择项。
请协助。再次,我可以让它工作,但我宁愿不要在视图中调用方法。当收集给它,即,@categories
设置为nil
可以共享错误堆栈跟踪 –
我猜测'Question.editable_by(CURRENT_USER)'将返回零,即,为CURRENT_USER没有可编辑的问题。 –
@KirtiThorat,最好的方法是从'development.log'?并且,不,“Question.editable_by(current_user)”被认为是“Question.all”。该功能在控制器和视图中都可以正常工作。只有在提交后才会导致错误。 –