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}"] }) %>
Related
I have this rails code :
<% status_a = [ ["DRAFT", "DRAFT"], ["OPEN", "OPEN"], ["CLOSE", "CLOSE"] ] %>
<%= form_for(:dash_action, url: brokers_dashboard_path ) do |f| %>
<%= f.select(:select_status, options_for_select(status_a), {}, selected:'OPEN' %>
<% end %>
When it runs, it generates this HTML code :
<select selected="selected" name="dash_action[select_status]" id="dash_action_select_status">
<option value="DRAFT">DRAFT</option>
<option value="OPEN">OPEN</option>
<option value="CLOSE">CLOSE</option>
...
But what I expect is :
selected="OPEN" and not "selected"
Why the select method is not doing what I want ?
Try following code snippet, default value should be the parameter of options_for_select
f.select :select_status, options_for_select(status_a, 'OPEN')
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 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.
How to dispaly selected value in edit page.
in my form i need to display selected value. it is not selecting the value which i selected and submitted. else it shows "please select Study material".
This is my StudyMaterial model
class StudyMaterial < ActiveRecord::Base
TYPES = ['Question Paper', 'Book', 'Audio', 'Video']
enum study_material_type: TYPES
end
This is my 'form.html.erb'
<select class=" required form-control" name="study_material[study_material_type]" id="study_material_study_material_type" data-validation="required" data-validation-error-msg="Select study material">
<option value="">Please select study material</option>
<option value="Question Paper">Question Paper</option>
<option value="Book">Book</option>
<option value="Audio">Audio</option>
<option value="Video">Video</option>
</select>
How to dispaly selected value in edit page.
I am getting this error when i click edit studymaterial page
Please help me to solve this error
<%= form_for #study_material do |f| %>
<%= f.select :study_material_type, StudyMaterial::TYPES.map{|v| [v,v]}, selected: f.object.try(:study_material_type) , required: true, include_blank: "Select" %>
<% end %>
I think, you look something like that:-
<%= form_for #study_material do |f| %>
<%= f.select :study_material_type, StudyMaterial::TYPES, include_blank: "Please select study material", required: true %>
<% end %>
It will display selected value.
I have the following form code
<%= f.fields_for resource.paid_account do |pa| %>
<%= pa.collection_select :account_plan_id, #account_plans, :id, :name_with_price %>
<% end %>
that generates the following HTML
<select id="user_paid_account_account_plan_id" name="user[paid_account][account_plan_id]">
<option value="2">Lite ($10.00/mo)</option>
<option value="3">Professional ($20.00/mo)</option>
<option value="4">Plus ($30.00/mo)</option>
</select>
Is user[paid_account][account_plan_id] the right name? Shouldn't it be user[paid_account_attributes][account_plan_id]?
I ask because this is causing problems on the backend; my account_plan record isn't getting created.
Looks like this answer is yes. I manually changed the name like this:
<%= pa.collection_select :account_plan_id, #account_plans, :id, :name_with_price, {},
{ name: "user[paid_account_attributes][account_plan_id]" } %>
and now it works. Feels like a hack, though, so it seems like there must be a better way to do it.