Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control the size of the select box in Ruby on Rails 3?

I create the select box like this:

f.select(:my_select_name, options_for_select(...))

How could I control the size (width) of the select box ?

For input text field I do:

f.text_field(:field_name, :size => my_size)

but for "select" it doesn't work.

Please advise.

like image 735
Misha Moroshko Avatar asked Dec 13 '10 05:12

Misha Moroshko


2 Answers

In gist, I use:

<%= f.select(:my_select_name, [1,2,3,4,5], {}, :style => "width:70px") %>

Or if I am using something like Twitter Bootstrap, I use a class:

<%= f.select(:my_select_name, [1,2,3,4,5], {}, :class => "col-md-1") %>
like image 90
Ryan Avatar answered Sep 19 '22 19:09

Ryan


The basic synatx for select is

select(object, method, choices, options = {}, html_options = {})

Options are replace by the dropdown option values and you can replace the html_options for the width.

eg. <% select("company", "branch_id", Branch.all.collect {|b| [ b.name, b.id ] }, { :prompt => "Select" }, {:class => "companySelect" })

For select_tag you can use

select_tag(name, option_tags = nil, options = {}) 

Here the option is similar to html_options of select.

eg. <%= select_tag 'company_name', options_for_select(get_company_name), :class => "select1"%>

For more details please see select Tag and select

like image 39
ssri Avatar answered Sep 20 '22 19:09

ssri