I have the following form:
<%= form_for #user do |f| %>
<div class="form-group">
<%= f.label :extra_act %>
<%= f.select(:extra_act, [['Act 1', 1],['Act 2', 2], ['Act 3', 3]], class: 'form-control', required: true) %></br>
</div>
<%= f.submit 'Submit', class: 'btn btn-primary' %>
<% end %>
Is there a way to offer checkboxes instead of the drop down select menu here? I have found checkboxes on the ruby website, but I only want users to have the option of selecting one checkbox.
This will work with rails 4+
You want the user to select only one value so instead of check_box you need radio button. Here is how you can do it(syntax):
collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
Something like this with your code:
f.collection_radio_buttons(:extra_activity_id, ExtraActivity.all, :id, :title, html_options: { class: 'form-control' }
For rails 3 you will need to loop on the extra activities:
<% ExtraActivity.all.each do |ea| %>
<%= f.radio_button :extra_activity_id, ea.id %>
<% end %>
As you don't have this is database and you are using an array then do it like this, it may work:
f.collection_radio_buttons(:extra_activity_id, [['Act 1', 1],['Act 2', 2], ['Act 3', 3]], :last, :first, html_options: { class: 'form-control' }
You can either use a single select list (meaning multiselect attribute should be off) or a radio group.
Whichever suits your needs.
Related
I am getting my heads around many things RoR - by developing a small movie database that has actors and movies.
I successfully setup Ransack to do two things for me:
Following Chris' great tutorial on autocomplete https://gorails.com/episodes/global-autocomplete-search I now have a working search field in my navbar with dropdown, autoselect etc - all works like charm :)
Set up a separate "moviesearch" method in my mainpage controller - with corresponding form etc - working great too.
Code of moviesearch method:
#q = Movie.ransack(params[:q]
#movies_search = #q.result(distinct: true).includes(:genres, :actors)
However I now try to setup an "actorsearch" method - also in my mainpage controller - with corresponding form etc... doesn't work. It does reply the right index page (view/actors/index.html.erb), but it doesn't pass any of the search parameters on.
Code of actorsearch method:
#q = Actor.ransack(params[:q])
#actors_search = #q.result(distinct: true).includes(:movies, :actor_attributes)
When I check the console - the moviesearch method triggers something like this:
SELECT COUNT(*) FROM (SELECT DISTINCT `movies`.* FROM `movies` WHERE `movies`.`title` LIKE '%lord%' ORDER BY `movies`.`sort_title` ASC) subquery_for_count
But the actorsearch triggers only:
SELECT COUNT(*) FROM `actors`
Not sure what's wrong...
Update: Here's the form code for the actor search - omitted all html-only stuff (div's etc)
<%= search_form_for #q do |f| %>
<%= f.label :display_name_cont, 'Name contains', class: 'form-label mb-0' %>
<%= f.search_field :display_name_cont, class: 'form-control' %>
<%= f.label :gender_eq, 'Gender is', class: 'form-label mb-0' %><%= f.search_field :gender_eq, class: 'form-control' %>
<%= f.label :actor_attributes_id_in, 'Attributes', class: 'form-label mb-0' %>
<%= f.collection_select(:actor_attributes_id_in, ActorAttribute.order(:name), :id, :name, {}, { class: 'form-select bootstrap', id: 'attribute-select', multiple: true } ) %>
<%= f.label :born_gteq, 'Born between', class: 'form-label mb-0' %><%= f.number_field :born_gteq, class: 'form-control' %></div>
<%= f.label :born_lteq, 'and', class: 'form-label mb-0' %>
<%= f.number_field :born_lteq, class: 'form-control' %>
<%= f.label :rating_gteq, 'Actor rated at least', class: 'form-label mb-0' %>
<%= f.number_field :rating_gteq, class: 'form-control' %>
<%= f.label :movies_id_eq, 'Movies starring in', class: 'form-label mb-0' %>
<%= f.collection_select(:movies_id_in, Movie.order(:sort_title), :id, :title, {}, { class: 'form-select bootstrap', id: 'movie-select', multiple: true } ) %>
<%= f.submit class: 'btn btn-outline-primary' %>
<% end %>
And I didn't quite know what you mean with "puts params[:q] into the controller. I have already tried to check the params with outputting debug(params[:q]) in various places - for example, in my actors index page, it shows like this:
parameters: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
display_name_cont: john
gender_eq: ''
actor_attributes_id_in:
- ''
born_gteq: ''
born_lteq: ''
rating_gteq: ''
movies_id_in:
- ''
permitted: false
...which looks pretty similar to the output for movies... at least I couldn't spot any immediate issue here?
I have this code and I want to make it display with checkboxes instead of the dropdown menu. Can that be achieved? .
<%= f.select :job_type_cont,[['Full Time','Full Time'],['Part Time','Part Time'],['Permanent','Permanent']],{:include_blank => 'All....'},class:"form-control",id:"bed_room" %>
For that is neccesary a checkbox input
First you need in you controller receive you parameter like an array:
params.require(:model).permit(:others, job_type_cont: [])
And later, you should do an each, like
<% job_type_conts = ['Full Time', 'Part Time', 'Permanent']%>
<% job_type_conts.each do |type| %>
# Edit: Check id and for of label and checkbox input
<%= f.check_box( :job_type_cont, { multiple: true, id: "#{type.gsub(" ","-")}"}, type ) %>
<%= f.label :job_type_cont, type, for: "#{type.gsub(" ","-")}" %>
<% end %>
You can watch a good simple guide
Rails multiple checkboxes of Sophie Déziel.
How can I set default value in Rails select helper block?
<div class="field">
<label>Gender</label>
<%= f.select :gender, [], { prompt: 'Select gender', selected: 'Female' }, { :class => 'ui selection dropdown' } do %>
<% Subject.genders.keys.each do |c| %>
<%= content_tag(:option, value: c, class: 'item') do %>
<%= content_tag(:i, '', class: "#{c.downcase} icon") %>
<%= content_tag(:span, c) %>
<% end %>
<% end %>
<% end %>
</div>
I tried setting it with :selected option but it doesn't work.
I don't think if selected exist for f.select.
You can use options_for_select(values, value_selected)
Suggestion:
You can create a file named app/helpers/select_helper.rb. In this file, you create a function like this:
def subject_genders_values
Subject.genders.each do |c|
[c.value, c.value]
end
end
your function subject_genders_values can be re-used. And every time if you want a select box, you can create your function in this file.
Notice: add
include SelectHelper
in application_helper.rb
And your views:
<%= f.select :gender,options_for_select(subject_genders_values, 'Female') %>
The option Female will be selected if it's a part of the list.
I have a simple form, with a select tag, with options for the select. I want to be able to list those selected options on a view. The only way I know how to do this, would be to call the instance variable that has ActionItem.new(action_item_params) in it, with an attribute as the method (in this case, :purpose). In the view, this displays nothing, where I'd like it to display the select tag option the user selected. - New to OOP, Ruby, and Rails - likely a rookie situation. Will read face off in the meantime.
Form:
<div align="center"><h1><%= #client_name.name %></h1></div></br></br>
<div align="center"><strong>Communication Options:</strong></br></br>
<%= form_for(:action_item, url: {controller: "action_item", action: "create"} ) do |f| %>
<%= f.label("Purpose Of This Contact?:") %>
<%= select_tag(:purpose, options_for_select([['Initial conversation/pitch', 1], ['Follow up correspondence', 2], ['Additional selling point', 3], ['New benefits to convey', 4]])) %></br></br>
<%= f.label("Method Of Correspondence:") %>
<%= select_tag(:correspondence_method, options_for_select([['Send an email', 1], ['Tweet on Twitter', 2], ['Make a phone call', 3], ['Send direct mail', 4]])) %></br></br>
<%= f.label("Do I know the contact person? ") %>
<%= select_tag(:know_person, options_for_select([['Yes', 1], ['No (have to find out)', 2]])) %></br></br>
<%= f.label("this field will be added, ONLY if above field is NO: How could you find the contact's name?") %></br>
<%= f.text_field(:contact_name_answer, :size => 50) %></br></br>
<%= f.label("Additional notes") %></br>
<%= f.text_field(:additional_notes, :size => 50) %></br></br>
<%= f.submit("Store Action Item") %></br></br>
<% end %>
And the show page:
<h1>Action Item: </h1>
Added: <strong><%= #action_submission.additional_notes %></strong>, nice job.</br> </br>
Added: <strong><%= #action_submission.contact_name_answer %></strong></br></br>
Added: <strong><%= #action_submission.correspondence_method%></strong></br></br>
I actually found the answer to be: Just use 1 array, rather than an array of arrays. Now, the array looks like:
<%= f.select(:purpose, ['Initial conversation/pitch', 'Follow up correspondence', 'Additional selling point', 'New benefits to convey']) %></br></br>
This worked just fine. Thanks for reading anyway!
Try f.select instead of select_tag.
I have a nested form model in my Rails app. When I go to edit an object in the model, all of my fields (text_area, check_box, etc.) are populated with data from the database except for the select box. How can I get my select box to also populate when I am using the form to edit existing data from the model?
<%= form_for #test do |f| %>
<%= f.fields_for :questions do |builder| %>
<%= render 'question_fields', f: builder %>
<% end %>
<% end %>
_question_fields.html.erb
<fieldset>
<%= f.text_area :question_text %>
<%= f.select :correct_answer, options_for_select([['A', 1], ['B', 2], ['C', 3], ['D', 4], ['E', 5]]), :prompt => 'Choose the correct answer' %>
<%= f.check_box :question_check %>
</fieldset>
You need to send the selected parameter in options_for_select function call.
<%= f.select :correct_answer, options_for_select([['A', 1], ['B', 2], ['C', 3], ['D', 4], ['E', 5]], :selected => f.object.correct_answer), :prompt => 'Choose the correct answer' %>
Try setting a value for selected.
Refer to: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select
Edit: Can't test it now but I'm pretty sure select will take the array of options by itself. So you shouldn't need options_for_select here - then selectshould set the selected option..
Try this and refer to: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select