我正在尝试创建一个select输入,它接受特定的用户模型,并将它们显示为字符串,同时将它们保存为整数。通常,我会将可枚举设置为与下面相同的方式,但设置为静态散列。
在尝试创建新产品时,我一直收到以下错误:“未定义的局部变量或方法`user_business_hash'”
我尝试过将'user_business_hash‘方法移动到应用程序/产品控制器、应用程序/产品助手,而没有成功。
模型
enum user_id: user_business_hash
validates :user_id, inclusion: user_ids.keys
def user_business_hash
# output: {User.business_name => User.id }
# ex: {"Business Name A"=>2, "Business Name B"=>1, "Business Name C"=>5}
array = User.where(account_type: 'Business').map{|x| [x.business_name, x.id] }
hash = array.inject({}) do |memo, (key, value)|
memo[key] = value
memo
end
return hash
end
表单
<%= form.select :user_id, Product.user_ids.keys, prompt: 'Select', id: :product_user_id %>
发布于 2017-07-06 23:39:40
我认为你真正想要的是:
在你的控制器上
@options = User.where(account_type: 'Business')
对你的看法
options_from_collection_for_select(@options, "id", "business_name")
https://stackoverflow.com/questions/44960319
复制相似问题