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?
Related
I have a select_field on a form such as:
<%= f.select(:task_id, Task.all.collect {|p| [p.name, p.id]}, {prompt: "Select"}, {class: 'form-control', required: true}) %>
The generated HTML is:
<select class="form-control" required="required" name="company[task_id]" id="company_task_id">
<option value="">Select</option>
<option value="3">Site Management</option>
<option value="1">Real Estate</option>
<option value="2">Meeting</option>
<option value="4">Training</option>
</select>
I want to add some extra text to the label such as:
Site Management (ABC)
Real Estate (ABC)
Meeting (ABC)
Training (ABC)
How do I add (ABC)?
Define a method in the Task model which will do that for you
#task.rb
def name_with_abc
"#{self.name}" + "(ABC)"
end
And now change the select to
<%= f.select(:task_id, Task.all.collect {|p| [p.name_with_abc, p.id]}, {prompt: "Select"}, {class: 'form-control', required: true}) %>
For more organised way It can be done as : -
in controller -
#tasks_options = Task.distinct.pluck(<<-PLUCK, :id)
CONCAT_WS("", tasks.name, " (ABC)")
PLUCK
which will fire a sql query
SELECT CONCAT_WS("", tasks.name, "(ABC)"), `tasks`.`id` FROM `tasks`
In view
<%= f.select(:task_id, #tasks_options, {prompt: "Select"}, {class: 'form-control', required: true}) %>
So instead of using collect which will iterate each element of array , this can be better solution.
<%= f.select(:task_id, Task.all.collect {|p| ["#{p.name} (ABC)", p.id]}, {prompt: "Select"}, {class: 'form-control', required: true}) %>
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.
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'm not sure why, but my form is not showing the options selected on submit, even though the params hash shows that the information is being returned to the page.
Collection select code:
<%= f.collection_select :post_topic_ids, PostTopic.all, :id, :name, {}, { multiple: true, class: 'form-control' } %>
Which renders:
<select multiple="multiple" class="form-control" name="post[post_topic_ids][]" id="post_post_topic_ids">
<option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option>
</select>
Params returned after form validation error
params = {"post"=>{"post_topic_ids"=>["", "1"]}}
Update
I have also tried:
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }), multiple: true %>
and:
<%= select_tag 'post_topic_ids', options_from_collection_for_select(PostTopic.all, "id", "name"), multiple: true %>
Which renders:
<select name="post_topic_ids[]" id="post_topic_ids" multiple="multiple"><option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option></select>
you need to specify which element is selected a third parameter
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }, --->selected_element<---), multiple: true %>
look at http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select for some examples.
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}"] }) %>