# Apply style in collection\_select?

**URL:** <https://www.ruby-forum.com/t/apply-style-in-collection-select/158806>\
**Category:** Rails\
**Created:** [January 28, 2009, 8:06am UTC](https://www.ruby-forum.com/t/apply-style-in-collection-select/158806 "2009-01-28T08:06:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dharmdip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@dharmdip](https://www.ruby-forum.com/u/dharmdip)\
**Post date:** [January 28, 2009, 8:06am UTC](https://www.ruby-forum.com/t/apply-style-in-collection-select/158806/1 "2009-01-28T08:06:53Z")

</div>

Hi to all,  
i have this snip i want to apply style to this combo but there is no  
effect  
help…

\<%= collection\_select(:customer,:country\_id, @countries,:id, :country,  
html\_options={ “style” =\> “width:110px” },:onchange =\>  
‘updateState(’’);’,:id =\>‘country\_id’ )%\>

Thanks,

---

<div class="post-metadata">

**Author:** ![dharmdip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@dharmdip](https://www.ruby-forum.com/u/dharmdip)\
**Post date:** [January 28, 2009, 8:18am UTC](https://www.ruby-forum.com/t/apply-style-in-collection-select/158806/2 "2009-01-28T08:18:26Z")

</div>

Hi,

Try this:

\<%= collection\_select(:customer, :country\_id,  
@countries, :id, :country, {:style =\> ‘width:100px;’, :onchange =\>  
“updateState(‘’);”, :id =\> ‘country\_id’}) %\>

Argument list is:

name of instance variable (@customer becomes the symbol :customer),  
name of attribute (@customer.country\_id becomes :country\_id), then the  
array of objects (@countries), then the id method to send each of  
these (ie send them all “id” to get the id of each object), then the  
display method (“country” in this case, which seems a bit odd to me,  
it’d probably be name rather than country… ), then a HASH OBJECT  
(represented by {} braces) which has html options in it.

The html\_options= simply assigns a temporary variable to be the  
hash… so you don’t need it, because you’re not using it elsewhere.

Good luck!

[http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
[http://random8.zenunit.com/](http://random8.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![dharmdip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@dharmdip](https://www.ruby-forum.com/u/dharmdip)\
**Post date:** [January 28, 2009, 8:34am UTC](https://www.ruby-forum.com/t/apply-style-in-collection-select/158806/3 "2009-01-28T08:34:32Z")

</div>

Thanks a lot ,  
but it is not working here actually your suggested code is breaking my  
java script and style problem is still there , but never mind . Actually  
here this issue is looking some critical because i can not see html  
generated code in source when i write this

\<%= collection\_select(:customer, :country\_id,  
@countries, :id, :country, {:style =\> ‘width:100px;’, :onchange =\>  
“updateState(’’);”, :id =\> ‘country\_id’}) %\>

help…

---

<div class="post-metadata">

**Author:** ![dharmdip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@dharmdip](https://www.ruby-forum.com/u/dharmdip)\
**Post date:** [January 28, 2009, 11:43am UTC](https://www.ruby-forum.com/t/apply-style-in-collection-select/158806/4 "2009-01-28T11:43:52Z")

</div>

On 28 Jan 2009, at 07:34, Dharmdip R. wrote:

> “updateState(’’);”, :id =\> ‘country\_id’}) %\>  
> It’s the usual “the first hash is options, the second is for html  
> options” thingy.
