How to send two values with a rails collection_select - ruby-on-rails

I want to send two values with my collection select. Currently, i'm saving the ID but I want to have access to the name in my controller as well.
= f.collection_select :foo_id, #foos, :id, :name
That's my code, pretty simple. Just having trouble getting access to that name.
The collection also comes from a external API, so I don't want to have to touch the API again to get the name.

As, I said in the comment. You can customize the <option> tag values like :
= f.collection_select :foo_id, #foos, ->(ob) { "#{ob.name}|#{ob.id}" }, :name
Now, inside the controller just split it the value on |, and use it.
This is just an idea out of millions.

Related

rails - getting a pair of values from select_tag and option_groups_from_collection optgroup

Is it possible to make select_tag return a pair of values (group_id, item_id) using option_groups_from_collection_for_select?
= select_tag :item_id,
option_groups_from_collection_for_select(#groups, :items, :name, :id, :proper_name),
class: 'form-control'
Each group has many items association, each item can belong to one or more groups.
I'd like to make the helper to add group_id to optgroup id attribute in addition to adding item_id to each item option and fetch those two on selection to send them to a controller, but I can't figure out how, nor is it even possible (or proper..).
I could make two different dropdowns, where 2. one would be loaded using ajax after selecting an option in the 1. one, but this just doesn't seem right..
EDIT
I've figured that I can pass a proc as a value_method, but unfortunately I'm unable to fetch group_id in there..
= select_tag :item_id,
option_groups_from_collection_for_select(#groups, :items, :name, ->(el) { "#{el.id}" }, :proper_name),
class: 'form-control'
Kind regards!
EDIT
Ok, I've finally figured it out, thanks to https://stackoverflow.com/a/6374301/19174916 :)
Ok, I've finally figured it out, thanks to https://stackoverflow.com/a/6374301/19174916 :)
Used grouped_options_for_select and passed group_id as a data attribute to each option, which I'd pass along with url later using simple JS.

Ruby on rails - bind a selected value to a select element for update

I can not figure out why this is not working, I saw a couple of examples and seems to be right.
I have two clases that are related, consultant and salary (a salary belongs_to a consultant).
The problem I have is, when I want to edit a salary, the consultant that appears on the form is not bind to the select (in the select it just appears the list as if I was creating a new one)
<%= f.select :consultant_id, options_for_select(Consultant.active.map{|s|[s.first_name, s.id]}, params[:consultant_id]), {}, class: 'form-control' %>
I think you should check your route that the :consultant_id param is available on this page. Else you may need to change the params[:consultant_id] to something like salary.consultant_id
Alternatively, you can use the collection_select method as such:
f.collection_select(:consultant_id, Consultant.active, :id, :first_name, prompt: true, class: 'form-control')
Let me know whichever works for you.
Note: It's not best practice referring to domain object within your view.

How to set-up Rails form select for a collection

I'm working on a code base that I'm not very familiar with, specifically Haml. I need to set-up a select dropdown to select a user.
I have the following code in my controller:
def edit
#franchise = Franchise.find params[:id]
#ab_reps = User.where role: "admin-ab"
authorize! :update, #franchise
end
I have the following code in my form (that doesn't currently work):
= f.select :ab_rep, options_for_select(#ab_reps, f.object.ab_rep), {prompt: "AB Representative"}, {label: false, right_class: "col-sm-10", class: "ab-rep-field"}
Couple questions:
1.) #ab_reps is an array of user objects. I have the following method in my user model:
def name
[first_name, last_name].compact.join(" ")
end
How do I get the select to display the user names instead of the user objects (which it currently does) ?
2.) Is my current set-up even close to being correct?
Thanks for your help!
You are close, you need to provide the methods for the option value and the option text, as well as the collection which in your case is #ab_reps. Additionally you can provide a hash for prompts and for html_options such as class names, which you've done.
Rails has a few different helpers you can use for select tags including options_from_collection_for_select. I've used collection_select often, http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
= f.collection_select :ab_rep, #ab_reps, :id, :name, {prompt: "AB Representative"}

How do I properly use a select box with ruby on rails?

I have a select box for the event types of an event. event belongs to event_type and event_type has many events. Here's what I have for the select box:
<%= f.select :event_type, options_from_collection_for_select(EventType.all, :id, :name, #event.id), :placeholder => 'Select an event type' %>
But, the problem is that when I submit it, it submits the id of the event type and not the name of it. So, how would I make it submit the name rather than the id? If you need to see the any more code than just tell me, thanks
The second parameter to options_from_collection_for_select is the value that will be submitted with the form. You have :id, so change it to :name.
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select
(but this seems like a strange thing to do - typically you would store the event type ID.)
You can use the id after the submit to load the event type again in your controller post action like this:
selected_type = EventType.find(params[:event_type]
It is also a good practice to keep database calls to the controller, so please put the EventType.all statement in there and pass it as local or class variable like you did with event.
If you really want to pass the name in your form instead of the id, you can replace the :id part in your call to something more like this options_from_collection_for_select(#event_types, :name, :name, #event.event_type.name). Keep in mind that this value should be unique!
The method works like this:
options_from_collection_for_select(collection, value_method, text_method, selected = nil)
So the first parameter contains all the options, the second defines the value within those option objects which are put into the value field of the HTML option (which is being submitted by the form), the third defines the text which is displayed to the user and the final parameter defines the value of the selected entry (in case you are editing an entry for example). For the last parameter, you need to use the events' event_type id, or in your case, the name because you set the value of your HTML tag to it.
Use pages like ApiDock or the Rails tutorials to get examples for some of these methods.
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select
You should pass name in the Value method, if you want to pass the name,
<%= f.select :event_type, options_from_collection_for_select(EventType.all, :name, :name, #event.id), :placeholder => 'Select an event type' %>
Here is the doc for options_from_collection_for_select

observ_field with collection_select on rails 2.3.8?

I'm developing project on rails 2.3.8 and I need to observe field on drop down menu which develop using collection select. Please can some one explain how to observe field ?
My collection select code is like this
<%= collection_select("event", "trainer_id", #trainers , :id, :name, {:prompt => true}) %>
And I don't know how to use observe field for this. So please can some one explain about this ?
Related: Auto populate a text field based on another text field
observe_field(field_id, options = {})
Observes the field with the DOM ID specified by field_id and calls a callback when its contents have changed. The default callback is an Ajax call. By default the value of the observed field is sent as a parameter with the Ajax call.
Read this details: http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/observe_field

Resources