Uppercase collection with simple_form - ruby-on-rails

How would one capitalise the first letter of a list of country's in below code example?
My question is regarding simple_form, not a regular form for which one could use collection_select.
= p.input :state,
:readonly => "readonly",
:collection => DataState.where(:country => "1"),
:selected => 1,
:id => "state",
:name => "state",
:prompt => "Please choose"
The problem is the list of states is in the database and saved like "alamaba","new york"
I need it to be down cased in the database but would like to uppercase/capitalize the first letter for visual clarity.

Try this:
In your DataState model, add a method that displays a capitalized version of your state names
class DataState < ActiveRecord::Base
def capitalized_state
state.capitalize
end
end
then use this new method to list your choices
Depending on if you're trying to save the id of the state as an association or just the string, the erb would be either
<%= f.input :state, collection: DataState.where(:country => "1").pluck(:capitalized_state) %>
or
<%= f.input :state, collection: DataState.where(:country => "1"), value_method: :id, label_method: :capitalized_state %>
where the former is for the state name and the latter is for the association id.
see this: using capitalize on a collection_select
Edit: I noticed that your html does not specify what attribute of the DataState class you're using as the text for your choices. If you're using rails, you should look into the collection_select form helper.

One way of going about it, too, would be the addition of the text-transform: uppercase property of css, looking like this:
<% = f.input: state, collection: DataState.where (: country => "1"), style: 'text-transform: uppercase;' %>

Related

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

Rails - Simple Form - Force a non-multiple select input field

I am creating an input form for a Post model with Simple Form on a Rails app. The Post model is associated to a Keyword model with a has_and_belongs_to_many. To fill up the Tags in the form, I am using:
<%= f.association :keywords, collection: Keyword.all(order: 'name'), prompt: "Select keyword..." %>
which creates a html :select tag for the input. The problem is, because is a many to many association, Simple Form assigns the :multiple tag to :select by default, allowing selecting many objects. But I do want to force it to output a simple <select> with no multiple for this field.
Any idea how to do this? Thanks a lot!
Figured it out, pass :input_html => { :multiple => false }
<%= f.association :keywords, collection: Keyword.all(order: 'name'), prompt: "Select keyword...", :input_html => { :multiple => false } %>

How to use a table column in my select box on rails?

I'm trying to create a select box that takes data from my db. I'm having trouble setting this up. I tried this code:
<%= f.fields_for :unit do |u| %>
<%= u.label :name %>
<%= u.select :name, :class => "ingredient_unit", :prompt => "Please Select" %>
<% end %>
but I'm missing the part of the choices, I don't know how to pull them out of the database. I tried using collection_select, which worked, but then the class option wasn't working... collection_select went like this:
<%= u.collection_select :unit, Unit.all, :id, :name, :class => "ingredient_unit", :prompt => "Please Select" %>
I also don't understand what the first symbol means (:unit), it seems to be setting the html id and name, so that can be anything I want it to be?
You should look at the documentation for collection_select and select. But to answer your question, for the select part, you forgot to pass the list of options to choose from. You also need to swap the order for prompt and class since prompt is an option for the helper and class is an html option
<%= u.select :unit_id, Unit.all.map { |u| [u.name, u.id] }, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
For the collection select
<%= u.collection_select :unit_id, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
The first parameter passed to both helper is the column name where you want the selected answer to be saved. The 2 codes above just shows 2 different ways to generate the same select tag.
The first symbol tells it which field to populate with the id returned from the user selection.
Also, you should wrap your class section in {}
:unit refers to the model attribute that you're using for the select element. Yes, it will setup the name/id of the element (and name is the most important for the params hash).
To set a class in the collection_select, specify it as a hash as that helper takes it as an html_option.
<%= u.collection_select :unit, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>

Rails: dependent collection_select fields

I have two fields in my Rails application: language and word. The words table is quite large, and as such I'd like to have the selection filtered by the choice of language. Below is code from the edit view and you can see how I have hard-coded in the language in the second collection_select.
.field
= f.label :language_id
= f.collection_select :language_id, Language.find(:all,:conditions => ["supported = 't'"]),:id,:language_code, include_blank: false, :title => 'Language'
.field
= f.label :word
= f.collection_select :word_id, Word.find(:all,:conditions => ["language_id = 2"]), :id, :word, include_blank: false, :title => 'Word'
A similar question was posted on SO, but the solution provided was to use AJAX. I would prefer to reduce the database load by adding a criteria after the user can selected the language.
Rails dependent collection_select fields in form
Is it possible for the Rails code to assess the chosen value of :language_id ?
Assuming the object containing the fields is Article :
= f.select :word_id, Word.where(:language_id => #article.language_id).map {|w| [w.name, w.id]}

ruby on rails using scope in a pick list

I have a code table called priorities. The table has a boolean called archive. I have a request table and it's form contains a pick list of priorities. I'm using scope statements to tell if a priority is archived or not. This is the code in the model:
scope :archived, where(:archive => true)
scope :active, where(:archive => false)
I'm trying to use the scope in the request form like this:
<%= f.association :priority, :label_method => :prioritycode.active, :label => 'Priority' %>
But, that doesn't work.
What should I be using in the form?
Thanks!
I may be missing something -- not familiar with the association helper you're using.
If you want a drop-down select list, I would do something like
<%= f.collection_select :priority, Priority.active, :id, :name %>
(Assumes you have a model Priority in which the scope :active is defined, and that a priority has a name. Your mileage may vary.)
tharrison post got me thinking (thanks) - this worked:
<%= f.association :priority, :collection => Priority.active.all, :label_method => :prioritycode, :label => 'Priority' %>

Resources