Rails 3 - collection_select - Understanding PROMPT? - ruby-on-rails

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 :)

Related

simple_form show selected option

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

collection_select issue ruby on rails

i'm new with ruby on rails and i've an issue.
In my app, an user can publish an idea and ideas belongs to activities.
So user can choose with a select which activity they want.
Here's my code for my form, i used collection_select
<div class="field">
<%= collection_select(:idee, :id, Activite.all, :id, :nom, prompt: true) %></div>
The select input work, i've all my activities in the select input but when i select activity and publish my idea, the idea don't take the value and stay empty.
Even when i edit idea, the idea don't take the value of the activity.
How can i resolve this ?
Given that you have an Idea and an Activity model and that you are building a form for an idea, I think you should write the collection_select as:
<%= collection_select(:idea, :activity_id, Activity.all, :id, :nom, prompt: true) %>
I suggest you to use form_for syntax if you are not already doing it.
check apidock for reference:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

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 %>

How to include a "Please select..." (default/prompt) in a grouped dropdown list?

The code I am using for the dropdown list is this:
<%= f.select :post_type_id, option_groups_from_collection_for_select(#categories, :post_types, :name, :id, :name) %>
It neatly divides the options into optgroups.
But how do I modify the code to include a prompt (or a default value) of "Please select..." ?
It seems hard to do with grouped dropdowns.
(The rails docs seem to suggest using a hash, but I've tried several alternatives without success.)
Bah, right after I posted the question I found the answer was in the docs for select, and not under option_groups_from_collection_for_selectdocs where I had been looking.
The answer is:
<%= f.select :post_type_id, option_groups_from_collection_for_select(#categories, :post_types, :name, :id, :name), :include_blank => "Please select..." %>

Resources