Rails collection_select默认选项? [英] Rails collection_select default option?

查看:296
本文介绍了Rails collection_select默认选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我把一个集合选择。

 <%= collection_select:PriceRange,7 .where('value> 0'),:value,:name%> 

我想获得默认选择为PriceRange,id为7,并且不依赖于任何用户设置,即其按照价格范围更改页面上显示的项目的表单的一部分。

  * * *更新的效果* * * 

我添加了

  @price_higher = PriceRange.find(7)

到处理视图的控制器,并添加

 ,{:selected = @ price_higher.value} 

它似乎做的伎俩,虽然是在寻找一个不太复杂的方法做所有内部的collection_select。

解决方案

添加:selected 选项。



示例:

  collection_select(:post,:author_id,Author.all,:id,:name_with_initial,{:selected =>whatever_value})
pre>

示例来自: ApiDock



在您的情况下:

 <%= collection_select:PriceRange, 7,PriceRange.where('value> 0'),:value,:name,{:selected => whatever}%> 


so I'm putting together a collection select.

<%= collection_select :PriceRange, "7", PriceRange.where('value > 0'), :value, :name %>

I'm trying to get the default selection to be PriceRange with the id of 7, this is independent and doesn't rely on any of the users settings, its part of a form that changes the items displayed on the page by their price range.

* * * UPDATED EFFORTS * * *

I added

@price_higher = PriceRange.find(7)

to the Controller that handles the view, and added

, {:selected => @price_higher.value}

inside the collection_select. It seems to do the trick, although was looking for a less complicated way of doing it all inside the collection_select.

解决方案

Add the :selected option.

Example:

collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:selected => "whatever_value"})

Example took from: ApiDock

In your case:

<%= collection_select :PriceRange, "7", PriceRange.where('value > 0'), :value, :name, {:selected => "whatever"} %>

这篇关于Rails collection_select默认选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆