Populate Two Columns in single collection_select Rails - ruby-on-rails

View
<%= collection_select(#table, "sp", #pops, "col2", "col2", {:prompt => "Select one"}) %>
Controller
#pops = Table.find(:all, :conditions=>{:col1 => "xyz"}, :order=> 'col2', :select=> 'DISTINCT col2')
This is my existing code. I am collecting a values in a column2 and populating it.
Existing populating values as (col2a,col2b,col2c)
Now, I wish to populate two columns col2 and col3 in a single collection_select. I wish to populate like
(col2a[col3a],col2b[col3b],col2c[col3c]). Kindly give me the idea to populate two columns in single collection select

In the Table model, add the following method (name it whatever suits your app, I name it "combined_value")
def combined_value
"#{self.col2}[#{self.col3}]"
end
In the view, the collection_select is as follows
<%= collection_select(#table, "sp", #pops, "combined_value", "col2", {:prompt => "Select one"}) %>
The generated HTML will look like this
<select name="table[sp]">
<option value="">Select one</option>
<option value="col2a[col3a]">col2a</option>
<option value="col2b[col3b]">col2b</option>
<option value="col2c[col3c]">col2c</option>
</select>
Is that what you want?

So you use col2 as keys and col3 as values? Hash[*col2.zip(col3)]

In the Table model, add the following method (name it whatever suits your app, I name it "combined_value")
def combined_value
"#{self.col2}[#{self.col3}]"
end
In the view, the collection_select is as follows
<%= collection_select(#table, "sp", #pops, "col2", "combined_value", {:prompt => "Select one"}) %>
The generated HTML will look like this
Select one
col2a[col3a]
col2b[col3b]
col2c[col3c]
Is this I want... My questions may confused someone... Sorry for that... God thanks to Hoa... Its Working as my requirement...

Related

Dropdown list in a column of Active admin

As an active admin column i need a dropdown list. I've done
column "Action" do
select :design, :collection => options_for_select(['a','b','c'])
end
it only shows b and c,
but option a is not showing in the dropdown list. Why? How can I solve that?
If I give ['ad', 'as', 'dsa', 'asfs'] the last three values are showing. The first one 'ad' is vanished.
I came across almost the same problem, so using your example
column "Action" do
select :design, :collection => options_for_select(['a','b','c'])
end
Gave the html
<select collection="<option value="a"="">a
<option value="b">b</option>
<option value="c">c</option>
design
</select>
Which like your question pointed out, is only showing options b and c
I don't know why it's always putting the first option tag inside the select tag but if anyone has the answer, please share!
But, the way we worked around that was by setting each <option> tag ourselves
select :design do
%w(a b c).each do |opt|
option opt, :value => opt
end
end
Which gave us the html
<select> design
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
Which shows the options a, b, and c
I'm not sure if this is the answer you are looking for but I hope this helps.
is this for a form?
if that's the case it should go something like this
f.input :design, :as => :select, :collection => ["a","b","c"]
where f would be the parameter for the form
i'm guessing you are having css issues because is not define for a column case
I am using rails 6 and active_admin 2.7.0
I don't know why would one complicate things if this can be done so simply as:
form title: 'Action' do |f|
input :design, collection: ['a', 'b', 'c']
end
I hope there is nothing wrong with it because I am doing this and works fine for me! Hope this helps somebody :)

Match an option in select form with boolean values

I have the following piece of code
%br
= f.label :active, 'Status'
= f.select :active, ['Active','Inactive']
Symbol :active is a boolean type var. How can i match Active => 1/True, and Inactive => 0/False , for the database adding.
Sorry for the newbie question, but i can't figure it out.
You can provide a pair of values for each options: first will be used as label (inner text of <option> tag), second will be used as a value attribute:
= f.select :active, [['Active', true], ['Inactive', false]]
It'll render something like:
<select name="model[active]">
<option value="true">Active</option>
<option value="false">Inactive</option>
</select>
Have a look at the docs for select and options_for_select.
A small extension of the earlier answer, if you're using a dropdown.
I needed to use "options_for_select." Also, ":selected" stores the value for the next time you return to the form.
<%= f.select(:active, options_for_select([['Active', true], ['Inactive', false]], {:selected => #symbol.active}),:prompt => "Select") %>

Using select method in form_for in rails

I would like to use a method inside a form_for in rails to create a select tag with options where the value of the options come from one array and the options enclosed by the option tags come from another array.
For example, the first option would be:
<option value = Array1[0]> Array2[0] </option>
How do I do this? Can I use 'select' such as:
= form_for #activity do |f|
= f.select(Array1, Array2, {:selected => nil, :prompt => 'Select Stage'})
I couldn't get something like this working, even though this format seemed consistent with options_for_select as described in the rails API at api.rubyonrails.org.
Try this:
= f.select(:method, Array2.zip(Array1), { :selected => nil, :prompt => 'Select Stage' })
The zip method will combine two arrays to make one two-dimensional array.
So, for example, [1,2,3].zip([4,5,6]) will return [[1,4], [2,5], [3,6]].
select can then interpret this as a list of option texts and option values.
Given [['Male', 'm'], ['Female', 'f']], select will return
<option value="m">Male</option>
<option value="f">Female</option>

Rails 3 Select Box helper, how to add custom options at the bottom that are static

How can I create a select box that has the following given 4 results for User.departments:
Department.title (Department.abbreviation)
Department.title (Department.abbreviation)
Department.title (Department.abbreviation)
Department.title (Department.abbreviation)
-----
Add New Department
Based on the following models:
User.department_id
Departments (id, title, abbreviation)
What I can't figure out is how add the two options at the bottom that say Add New Department.
Here is what I have so far:
<%= collection_select(:user, :department_id, Department.where(:id => current_user.department_id), :id, :title, {:prompt => true}) %>
Thanks
Your best bet is to use select instead of collection_select for this, here is an example of how I would do it.
<%= f.select(:user, Department.where(:id => current_user.department_id).collect {|p| [[p.title,' (', p.abbreviation,')'], p.id] } + ['Add New Department']) %>
Then you could use something like javascript to do something when 'Add New Department' got selected or however you planned on using it.
Hope this helps and happy coding.

Rails FormTagHelper missing important select and collection_select methods

You can do this when you use form_for(#model...):
collection_select(:subscription, :duration, ["Some", "Values"], :to_s, :to_s, {:prompt => true})
And the output is something like this:
<select id="subscription_duration" name="subscription[duration]">
<option value="">Please select</option>
<option value="Some">Some</option>
<option value="Values">Values</option>
</select>
If you use a form without a model, you don't have that nice helper method to create the <option> tags for you. Instead, you have to do this:
select_tag("subscription", '<option value="Some">Some</option><option value="Values">Values</option>')
FormHelper and FormOptionsHelper work together on a form wrapping a model, and they have the select and collection_select to make life easy. For a plain form_tag (without a model), however, there is no such FormOptionsTagHelper. FormTagHelper has a select_tag method, but you have to manually write out the options which is a waste. It seems like this has been fixed somewhere.
I could write my own helper to get rid of writing those option tags manually, but thats what FormOptionsHelper#collection_select does... Is there an equivalent out there for forms without models?
select and collection_select can be called without a model. I usually use a combination of two significant words, and an array of pairs [label, value] to select. The only drawback is having to use the format abc[xyz].
You tried to use options_for_select?
select_tag 'Account', options_for_select({ "Basic" => "$20", "Plus" => "$40" }, "$40")

Resources