Help with collection_select - ruby-on-rails

The following produces a working select drop down that pulls from my user model:
<%= f.collection_select(:user_id, #users, :id, :firstname, options ={:prompt => "Select a User"} %>
I also have a column :lastname.
I am trying to populate the select with something like :firstname + " " + :lastname
This obviously fails if I just stick it in where :firstname is. How would you go about concatenating the two columns and populating the select box.
Thanks.

In your user model create a new method called name. Then use it in your helper.
class User
def name
"#{firstname} #{last_name}"
end
end
<%= f.collection_select(:user_id, #users, :id, :name, :prompt => "Select a User") %>

define a method full_name on the User model and then use :full_name in the collection select

Related

collection_select show multiple object attributes?

In addition to showing the challenge's name. I also want to show its deadline next to the name.
It would look like this for example:
Visit London 09/09/16
Make $1,000,000 10/15/18
Knit a Scarf 01/11/19
Instead of just this:
<%= f.collection_select :challenge_id, current_user.challenges.order(:deadline),:id,:name, include_blank: true %>
Define a method name_with_deadline in challenge.rb
def name_with_deadline
"#{name} #{deadline}"
end
and then make use of this method as label in the collection.
<%= f.collection_select :challenge_id, current_user.challenges.order(:deadline),:id, :name_with_deadline, include_blank: true %>
The name_with_deadline method will called for every object in the collection to retrieve the label text.
Hope this helps!
You can add a virtual attribute to you model like below:
def name_deadline
"#{name} #{deadline}"
end
collection_select:
<%= f.collection_select :challenge_id, current_user.challenges.order(:deadline),:id,:name_deadline, include_blank: true %>

Getting value of an object in collection select

I have created drop down list for employees.
What I want?
I want to select full Name for each one of them.
Type of form:
I use simple_form.
I actually have:
= f.input :person_id, label: "Employee", collection: #employee, prompt: "Select employee"
Result(I know, that is reference):
Before I use collection_select, but simple_form doesn't support validation for this type of collection.
Code for collection_select. This type of drop down list displays properly full name.
= f.collection_select :person_id, #employee, :id, :fullName, {prompt: "Wybierz pracownika"}, {class: "form-control"}
Update:
fullName is a method in a person.rb model.
def fullName
"#{first_name} #{last_name}"
end
Object employee.
#employee = Person.where.not(type: "Client")
The easiest way to do this is :
= f.select :person_id, options_for_select(#employees.map{|e| [e.fullName, e.id]}), {:prompt=>"Wybierz pracownika", :required => true}
It will show full names as select options and will dispatch ids as values with form.
You follow this code like below:
<%= f.collection_select(:person_id, Model.all, :person_id, :fullName,{:prompt=>"Wybierz pracownika"}, {:class => 'form-control'}) %>
You will replace your model_name.
Or
= f.input :person_id, label: "Employee", collection: #employee.fullName, prompt: "Select employee"
I think will help you

rails 4 -- concatenate fields in collection_select

I'm working on learning Rails 4 via several tutorials, and building a demo app.
I have a table called players that links to a team table. The team has many players, a player has only one team. So I'm using a collection_select tag to pull the team data into the player form.
It looks like this:
<%= collection_select :player, :team_id, Team.find(:all), :id, :name, options ={:prompt => "Select a team"} %>
This works fine-- but I'd like to have the format look like "Team Name: Team City"-- I can't figure out how to concatenate the :name and :city values in the tag however. Is this possible?
Create a method in your Team model like the following one
def name_with_city
"#{name}: #{city}"
end
Then use it as below
<%= collection_select :player, :team_id, Team.find(:all), :id, :name_with_city, {:prompt => "Select a team"} %>
Find out more about collection_select in the documentation
You can format the collection to your liking as:
<%= collection_select :player,
:team_id,
Team.find(:all).collect { |t| [ t.id, "#{t.name}: #{t.city}" ] },
:first,
:last,
{ prompt: "Select a team" } %>
The :id and :name parameters have been replaced with first and last signifying the value_method to be first and text_method to be last elements of each array.
If your form is looking up values based on parameters and you need those, the model method is not very convenient. Assuming your controller has a #searched_record method, you can do something like
<% #base_params = #searched_record.one_value << "=>" << #searched_record.second_value << "; " << (l(#searched_record.starts, :format => :long)) << ", " << #searched_record.other_value.description %>
<%= f.text_area :content, :rows => 5, value: #base_params %>

How to set a child attribute as collection_select value?

My form contains the following tag:
<%= f.collection_select :employee_id, #employees, :id, :value, :prompt => true %>
Employee looks like this:
employee
- attr1
- attr2
- user
- firstname
- lastname
My question: How to set the lastname of an employee as the value in the select field? I'm pretty sure it's possible, but I think I have some gaps in the syntax.
Why would you do that?
It's possible, with:
<%= f.collection_select :employee_id, #employees, :last_name, :text_method, :prompt => true %>
Where :text_method is method which called on #employees members will return text that you'd like to appear in dropdown.
You may be asking yourself why I have used <%= f.select %> this is FormOptionHelper on the Ruby on Rails Api this is very similar to collection_select however with that it returns and tags for the collection of values. Whereas select creates a series of contained option tags for provided object and method. So in saying this I believe you could have the following:
<%= f.select(:employee_id, Employee.all.collect { |emp| [emp.lastname, user.id] }
.sort{ |a, b| a[0] <=> b[0] }, {:prompt => "Select a Employee"}) %>
This selects all employees and sorts them in order of their last name.

Ruby on Rails: Concatenate two pieces of data into one column

I have 3 columns in my table projects. first_name, last_name, and fullname.
<%= f.hidden_field :first_name, :value => current_user.firstname %>
<%= f.hidden_field :last_name, :value => current_user.lastname %>
At the moment when the user saves a project, the first name and last name of user get saved into separate columns. I am now wanting for both the first name and last name to get saved to a column full name, so I can search on them later.
So if the name was
first_name = "Joe"
last_name = "Bloggs"
How would I get
fullname = "Joe Bloggs"
I tried this, but it doesn't work.
<%=f.hidden_field :fullname, :value => :first_name + :last_name %>
Can someone point me in the right direction? I'm new to rails so please remember this when trying to help. Thanks.
UPDATE
Now I have added this to my project model:
def set_fullname
fullname = first_name + last_name
end
and I now call this in my view:
<%= f.hidden_field :first_name, :value => current_user.firstname %>
<%= f.hidden_field :last_name, :value => current_user.lastname %>
<%= f.hidden_field :fullname, :value => #project.fullname %>
When I hit submit, and I check the logs the first and last names get saved to the table as usual, but the full name goes in blank. Can anyone see what the problem is?
UPDATE2
View:
<%= f.hidden_field :first_name, :value => current_user.firstname %>
<%= f.hidden_field :last_name, :value => current_user.lastname %>
<%= f.hidden_field :fullname, :value => #project.set_fullname %>
Project Model:
def set_fullname
fullname = first_name + last_name
end
When I try to access the page I get this error.
undefined method `+' for nil:NilClass
Ruby code in your view is executed when the form is loaded, by that time there is no content in the form (if you create a new record). It would make sense to write similar code in javascript an map it to onchange event first_name and last_name text field.
Or you can do it on server side in your model, e.g:
class Person
before :save, :set_fullname
def set_fullname
fullname = "#{first_name} #{last_name}"
end
end
before :save is executed each time when you're update your model
Anyway much cleaner solution would be to adjust your search query, this way you just duplicate information in your database.
While searching try something like this:
"first_name LIKE ? or last_name LIKE ? or concat(last_name, ', ', first_name) LIKE ?"
this SQL code might be database dependent, if you're using MySQL it's probably ok
I know this was already answered by Tombart, but this could help someone else.
class Person
before_create do
self.fullname = "#{first_name} #{last_name}"
end
end
You can find more info on RailsGuides.

Resources