Select helper not showing fields values - ruby-on-rails

I'm trying to build a form with select. I want the select to show the users name and map it the id of their db record. there trouble I'm having is instead of showing the names as the options is how the object id
#<User:0x007fac10de06c0>
this is the code is have used for the select helper
select :task, :assigned_to , #project.team.users{|user| [user.profile.name, user.id]

I think it should be #project.team.users.collect

Related

RUBY RAILS - Allow user to write in option that is not in drop down list

Still pretty new to ruby.. How would I add a feature to this drop down to allow users to enter an attribute that is not in the drop down list. Right now when I try to add something it says no results matched.
<%= f.select(:attribute_type, #attribute_types, {include_blank: true}, class: 'form-control selectpicker', data: {'live-search' => 'true'}) %>
The no results matched message is because you are not adding something to the select, but you are searching in the select list.
If you want users to have the option to fill in whatever they want instead of selecting it drop the select you can just add an input field below the select.

How do I get the previously selected value from the dropdown in trestle admin

I have two select fields and I want to show the second dropdown based on previously selected dropdown value
e.g:
form do |record|
...
select :app_ids, App.all.map{|app| [app.name, app.id]}, {label: 'App'}
select :role_ids, AppRole.where(app_id: app_ids).roles, {label: 'Role'} # Here I want to use the app_ids
Does trestle supports getting the value of previously selected value or should I need to use ajax if so how do I render the partial file in trestle admin

Rails pass id in selection instead of string

I have a select form:
<%= f.select :business_name, options_for_select(Client.uniq.pluck(:business_name)),{:include_blank => false},{:multiple=>true} %>
It picks out the distinct business_name and renders them in a selection box. I need the form to send the relevant business_id when a business_name is selected instead of sending the name string.
How do I achieve this?
options_for_select takes an array of arrays. Ideally if you want the name and value on the html select option to be different you pass in those pairs as [name, value]. By using pluck to grab only the business name you're passing in [name]--no value to put into the option tag.
change your code to use:
...options_for_select(Client.uniq.pluck(:business_name, :id))...>
You can use this:
options_for_select(Client.uniq(:business_name).collect{ |c| [c. business_name, c.id] })
So you would return all unique values on business name and the collect with name-value pair for select

Multiple values from f.select in rails

This shouldn't be a problem but I can't figure it out.
Using form helpers I populate a select field like so:
...
<%= f.select(:entity_id, entities.map {|entity| [entity.name, entity.id]}) %>
...
This is to create a User and set their entity_id. After the form submission I now have a new User whose entity_id is now the value of the option from the select field.
For example if I had selected "Store A" as my entity from the select element, the entity_id for that user is now '1' after I submit the form. This is great but I also need the entity_name "Store A" associated with the user.
So instead of my new user returning the following:
#<User name: 'John Doe', entity_id: '1', entity_name: 'Store A'>
I am getting
#<User name: 'John Doe', entity_id: '1', entity_name: nil>
Which makes sense becuase I never set entity_name.
How would I set entity_name in this form based on the selection ?
If I understand your question correctly, it looks like you want to set two values at the same time for a single input box. Rails does not provide a simple way to do this but it seems that this answer is similar to what you want.
However, I'd still ask the question: Why try to do it all on form submission?
You are already setting the entity_id on the User so you have a few options.
Assuming that entities is a relation of ActiveRecord or some other ORM objects, you could let the association do the work. If a user has_one :entity and then needs to know its entity_name then it could look like: user.entity.name after you set the correct entity_id on said user. This leverages the built in relationship framework Rails provides and keeps you from duplicating data.
If entities is not a relation or set of persisted objects, the code you used to generate the form must have known about entities during render time so it stands to reason that you would have access to it again. In the controller that accepts this form, fetch entities the same way you have done previously and link the entity with the submitted entity_id's name to the User you are updating.
If neither of these are feasible or make sense, go ahead and use the JSON encoded form values.
Try this
<%= f.select :entity_id,
options_from_collection_for_select(#entities, :id, :name, entities),
{},
{
data: { placeholder: "Choose entity..." },
multiple: true
} %>

1 select_tag multiple database columns

The background is this:
our end users are not the most tech suave so we try to make it as simple as possible for these guys.
We have a form where a reviewer will look at a record and qualify it. This data goes into multiple columns:
example:
Was it a lead?
based on the answer it'll bring yup 2 select tags that'll go to the following database column:
yes:
<td><%= f.select :reasoninquiry_id, options_from_collection_for_select(#inquiryreasons, 'id', 'reason', #lead.reasoninquiry_id), { include_blank: true } %></td>
No:
<%= f.select :nonleadaction_id, options_from_collection_for_select(#nonleadactions, 'id', 'non_lead_action_type', #lead.nonleadaction_id), { include_blank: true } %>
We give our end users the opportunity to update this if they want. And here is where it gets tricky.
Currently our app is written in PHP and we use 1 select box and it sends data to 2 different tables, depending on what was selected.
Is there a way I can do this in rails?
To be clear what we'd want is 1 select box that will be able to submit data to 2 database columns, depending on what was selected. I'd want one select tag for 2 collections.
The select box would show options for both #inquiryreasons (which would submit to :reasoninquiry_id if one of them is selected) and #nonleadactions (which would submit to :nonleadactions_id if one of them is selected.)

Resources