I have a select option in my rails application:
<%= f.select :willingToRelocate, ['Yes', 'No'] %>
It works fine, now I'm trying to style it with bootstrap, but can't get style to work.
This is whats on the bootstrap guide:
<select class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
And heres a link to the bootstrap styling doc: http://getbootstrap.com/css/
You could style it like this <%= f.select :willingToRelocate, ['Yes', 'No'], {}, class: 'form-control'} %>
You could check out this link. The api.rubyonrails have a great amount of information that you could use for these kind of problems.
Related
I have a form without a model backing it built using form_with in Rails 6:
<%= f.text_field :one %>
<%= f.select :two, [['Option 1',1],['Option 2',2]] %>
<%= f.submit 'Submit' %>
The only documentation I can find to set which of the select options are selected by default says that it will pre-select whatever is in the model. Since I don't have a backing model, how can I choose which option is selected? I've poked around the options a little and found nothing, but I do not necessarily know where to look.
You must have missed it, there's an optional selected keyword argument.
Lastly, we can specify a default choice for the select box with the
:selected argument:
<%= form.select :city, [["Berlin", "BE"], ["Chicago", "CHI"], ["Madrid", "MD"]], selected: "CHI" %>
Output:
<select name="city" id="city">
<option value="BE">Berlin</option>
<option value="CHI" selected="selected">Chicago</option>
<option value="MD">Madrid</option>
</select>
https://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease
I'm trying to create a Select element using Rails form_for helper. This is what I'm trying to create:
<label for="charge_occurrence">When to charge</label>
<select class="w-select" data-name="charge_occurrence" id="charge_occurrence" name="charge_occurrence" required="required">
<option value="monthly">Monthly</option>
<option value="episodic">Per episode</option>
</select>
I have tried several variations of this:
<%= f.select :charge_occurrence, [['Monthly', 'monthly'], ['Per Episode', 'episodic']], data: {name: 'charge_occurrence'}, html: {class: "w-input", maxlength: "256", required: "required"} %>
I can get everything to work except for the data-name part.
Try <%= f.select :charge_occurrence, [['Monthly', 'monthly'], ['Per Episode', 'episodic']], html: {'data-name' => 'charge_occurence', class: "w-input", maxlength: "256", required: "required"} %>
See: How to add data attribute in Rails form select tag?
I'm trying to include a boolean value as a dropdown box using the following code in my edit view:
<div class="col-md-8">
<%= f.select :match, options_for_select([['On', true], ['Off', false]]), class: 'form-control input-md' %>
</div>
However I'm experiencing two problems:
1.It does not display the correct value. Even when the user's value is false, it still displays On.
2.It does not implement the styling. The inspector shows that it implements it as follows:
<div class="col-md-8">
<select name="user[match]" id="user_match">
<option value="true">On</option>
<option value="false">Off</option>
</select>
</div>
What am I doing wrong?
Do it like this:
<%= f.select :match, options_for_select([['On', true], ['Off', false]], selected: your_object.match),{}, {class: 'form-control input-md'} %>
I am using bellow code and function options_from_collection_for_select for generating options for user.
<%= select_tag 'receiver', options_from_collection_for_select(#user, 'id', 'email') %>
Above code generate bellow html:
<select id="receiver" name="receiver" style="display: none;">
<option value="1">email1#yahoo.com</option>
<option value="2">email2#gmail.com</option>
<option value="3">email3#gmail.com</option>
</select>
But i want email with username, e.g <optionvalue="1">email1#yahoo.com(some_user)</option>
Suggestion any alternate function or customize current function will be appreciated.
In your User model add a method :
def user_dispay_name
"#{email}(#{full_name})"
end
Now do :
<%= select_tag 'receiver', options_from_collection_for_select(#user, 'id', 'user_dispay_name') %>
But without options_from_collection_for_select this function i'm using below code:
<%= select_tag 'receiver', options_for_select(#user.map{ |c| ["#{c.email} (#{c.display_name})", "#{c.id}"] }) %>
I have next string in my form template(from book 'Agile web development with Rails'):
<%= f.select :pay_type, Order::PAYMENTS_TYPES, promt: 'Select a payment method' %>
When form is being visited with browser, there is not default "promt" for select field indicated. What is the problem?
UPD I'm using Rails v4.0.2
UPD Html output(in the browser):
<div class="field">
<label for="order_pay_type">Pay type</label><br>
<select id="order_pay_type" name="order[pay_type]"><option value="Check">Check</option>
<option value="Credit card">Credit card</option>
<option value="Purchase order">Purchase order</option></select>
</div>
I think you misspelled it. It should be theprompt: not promt
<%= f.select :pay_type, Order::PAYMENTS_TYPES, prompt: 'Select a payment method' %>