Ruby on rails 3.1 选项:prompt=>true from f.select不处理编辑操作
我的编辑表单有点问题。由于某些原因,我的选择助手在编辑操作上无法正常工作。出于某种原因,它似乎忽略了选项:prompt=>true,并且没有显示“请选择”消息。但仅在编辑操作上,新操作工作正常 表单部分的代码段:Ruby on rails 3.1 选项:prompt=>true from f.select不处理编辑操作,ruby-on-rails-3.1,erb,Ruby On Rails 3.1,Erb,我的编辑表单有点问题。由于某些原因,我的选择助手在编辑操作上无法正常工作。出于某种原因,它似乎忽略了选项:prompt=>true,并且没有显示“请选择”消息。但仅在编辑操作上,新操作工作正常 表单部分的代码段: f.select :category_id, @categories, {:prompt => true} 编辑操作 def edit @page = Page.find(params[:id]) @categories = Category.where(:
f.select :category_id, @categories, {:prompt => true}
编辑操作
def edit
@page = Page.find(params[:id])
@categories = Category.where(:cat_type=>"page").map { |c| [t("category.#{c.slug}",:default=>"#{c.name}"), c.id] }
end
新行动
def new
@page = Page.new
@categories = Category.where(:cat_type=>"page").map { |c| [t("category.#{c.slug}",:default=>"#{c.name}"), c.id] }
respond_to do |format|
format.html # new.html.erb
format.json { render json: @page }
end
end
有什么想法吗
f.select :category_id, @categories, :include_blank => "whatever your prompt says"
这是我的工作
不过,我通常会尽量避免使用rails表单帮助程序进行提示,因为它们的行为似乎从来都不正确
或者,我是这样做的:
f.select :category_id, @categories.unshift(["whatever your prompt says", value])
将数组从Apidock推到@categories数组的前面:
select(object, method, choices, options = {}, html_options = {})
因此,这将起作用:
f.select :category_id, options_from_collection_for_select(@categories, :id, :name), {prompt: 'Please select ...' }, { class: 'form-control' }
为具有提示值和html选项类的方法的表单选择选项:
f.select(:subcription_with, options_for_select(['paypal', 'stripe']), {:prompt => 'Select Payment Method'}, {:class => "form-control slct-box"})
它按预期工作。如果以前选择并保存了某个值,则不会显示提示 提示与选择-主要区别在于,如果选择已具有值,则:提示将不显示,而:include_空白将始终显示
请参阅。不错的解决方案,我要花很长时间才能找到第一个解决方案!我在未定义的局部变量或“value”方法中遇到此错误