simple_form show selected option - ruby-on-rails

I want to show a select box with users, using simple_form.
currently my code looks like this:
<%= f.input :user, collection: get_members, label_method: :fullname %>
this is working, but the problem is that the select box doesn't show the selected option.
Does anybody know how to show a selected option with simple_form?
thanks for your help,
Anthony

You can set selected to the value of the user_id, but simple_form should be smart enough to figure this out by itself, so probably something wrong somewhere else (controller?):
<%= f.input :user, collection: get_members, label_method: :fullname, selected: member.id %>
Sorry, you probably need f.association:
f.association :user, collection: get_members

Related

Ruby on Rails 4 - simple_form multiple select input

I have a simple_form input field that looks like this:
<%= f.input :particular_users, collection: #all_users, input_html: { class: 'multiselectuser', multiple: true} %>
When I leave multiple: true off, the form submits the chosen value for the parameter :particular_users and I can see the value when debugging using "raise params.inspect". However when I leave the multiple: true option there, no vales get passed for the parameter :particular_users.
What am I doing wrong?
EDIT: I can not use the association input because :particular_users is a virtual attribute and has no relationship. I want the multiple select box to pass whatever values that are in there, even if they are arbitrary.
f.input :days, collection: #your_collection, input_html: { multiple: true }
It actually does work the way I wanted it to. The trick is to tell the strong parameters to allow a hash. It doesn't throw a strong parameters error, the param just gets thrown out and doesn't come through. So I set it to for example: params.require(:survey).permit(:particular_users => []).
To create multiple select tags with simple_form, use:
<%= f.association :particular_users, collection: #all_users, input_html: { class: 'multiselectuser'} %>
see part Associations in the gem description.
But as you don't want to use an ActiveRecord associaion, use select_tag:
<%= select_tag 'particular_users',
options_from_collection_for_select(#all_users, :id, :name),
multiple: true, class: 'multiselectuser' %>

rails Using simple form I would like to enable by textbox through a checkbox

I am currently trying to keep a checkbox in form which enables the text field up it is checked So how can I do it. I am using simple form.
this is simple form
<%= f.input :satisfaction %>
I have tried keeping as below
<%= f.input :satisfaction, as: :boolean %>
But is only displaying checkbox not a text field.
So can any one tell me how to this.
Your syntax is invalid. You've missed a comma between the two arguments. Try:
<%= f.input :satisfaction, as: :boolean %>

Rails collection_select "selected" value is cancelled out by the default "prompt"

Couldn't find any reference to my problem in the API, so here goes:
<%= f.collection_select :category_id, #categories, :id,
:name, {prompt: true}, { selected: #selected_value } %>
My users arrive to the form from different links, and depending on the link, they get their categories pre-selected for them from the #categories set. Sometimes they come from a general page, so instead of a pre-selected option they see the default prompt.
Problem: with my current code prompt replaces the selected value.
Thanks for any advice!
So I went with placing a conditional inside my collection_select and now it works fine
<%= f.collection_select :category_id, #categories, :id, :name,
#selected_category ? {prompt: "Your text"} : {selected: #selected_category} %>

Rails + Ransack - Drop Down List Collection?

I am loving the ransack gem for its flexibility, however I am unable to get the standard collection_select to function properly. Perhaps someone can assist.
Example:
<%= collection_select(:expense, :project_id, Project.all,
:id, :name, { prompt: 'Select Project'}, { class: 'span4' }) %>
in this case, this code is from an expense entry screen, so the first parameter is the expense object. What should it be in the ransack form? Also, I know I need to get the suffix in there. In this example, I would like project_id_eq to be the search pattern.
Also, my form is on a controller and view called "reports", I am not putting this search in the default controllers.
Thanks!
Seems that this will work.
<%= f.select :project_id_eq, options_from_collection_for_select(Project.all, "id", "name", #search.project_id_eq) %>
If anyone has another suggestion, would love to know it too.
To do this with an include_blank, put it outside of the parentheses:
ex:
<%= f.select :languages_id_eq, options_from_collection_for_select(Language.all, "id", "name"), include_blank: true %>
== UPDATE ==
better yet, use f.collection_select. This way after the user searches for something using the drop down, that option is preselected on the following page:
<%= form.collection_select :vendor_id_eq, Vendor.all, :id, :name, include_blank: true %>

Rails 3 - collection_select - Understanding PROMPT?

I'm building a form to allow a user to CRUD a project permission.
....
<% roles = Role.all %>
<%= f.collection_select :role_id, roles, :id, :name, :prompt => true %>
Problems with the above, while it renders:
If a value matches, it shows that in the dropdown as selected, which is good. Problem, is if a user is set as ADMIN. It's easy to use the dropdown to change the permission to something else, but not to CLEAR the permission...
Example... Select Drop Down:
- Please Select
- Admin
- Member
- Guest
If Admin is selected, Please Select never shows up.... How can I make an option show up to allow the user to remove the setting?
Any ideas? thx
I believe you want:
<%= f.collection_select(:role_id, roles, :id, :name, {:include_blank => 'Please Select'} %>
See the FormOptionsHelper docs for more information
<% roles = Role.all %>
<%= f.collection_select :role_id, roles, :id, :name, :prompt => (#user.admin? ? true : false) %>
does that help you?
you must be having a way to check if a user is admin / not.. use that condition in ternary operation to set the value of :prompt..
lemme know how it goes :)

Resources