Dropdown list in a column of Active admin - ruby-on-rails

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 :)

Related

Populate Two Columns in single collection_select 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...

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")

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use?
I can't see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a scenario where collection_select is better than select? or is anything I am missing here?
collection_select is intended to be used when the list of items is an array of ActiveRecord objects. collection_select is built on the top of select so it's a convenient method when you need to display a collection of objects and not an array of strings.
collection_select(:post, :author_id, Author.find(:all), :id, :name)
I have written something on that a while back, have a look at
http://nasir.wordpress.com/2007/11/02/not-binding-your-selection-list-to-a-particular-model-in-rails/
Hope that helps
And regarding select, you can use it with a Hash. I used to use it with ENUM.
# In a hypothetical Fruit model
enum types: { 'Banana' => 0, 'Grape' => 1, 'Mango' => 2 }
# In the view
f.select :type, Fruits.types.invert
Note that I had to use invert in order to show the correct value in option:
<select>
<option value="0">Banana</option>
<option value="1">Grape<option>
<option value="2">Mango</option>
</select>
To refer to it in a show file you can use Fruit.types and this will return our previous Hash. This way you can do:
Fruit.types[obj.type]
Last note: You can use symbols instead numbers if you prefer enum types: { 'Banana' => :banana, ...and you will get <option value="banana">Banana</option>

Using Multiple collection_select elements on a form with multiple models

I'm just learning rails and I've run into a bit of a snag. Let me start with a simple breakdown of my application - it's a cookbook (of sorts)
Recipes have one or more ingredients (tuna, spleens, etc)
Ingredients have one unit (ounces, pounds, etc)
Units are pulled from a lookup table
Here's a screenshot to help clarify things further:
Form Mockup
Here's my issue: my collection_select elements names should be something like unit[id][]
Instead, they're all just named unit[id]. Here's the snippet I'm using:
collection_select(
:unit,
:id,
#units,
:id,
:name,
options = {
:prompt => "Please Select",
:class => "ingredient_unit",
:name => "unit[][]",
:id => "unit:" + i.to_s()
}
);
However, this is what it is outputting:
<select id="unit_id" name="unit[id]">
<option value="">Please Select</option>
<option value="1">Ounces</option>
</select>
...
Now, in php, these dropdowns would be named unit[]. Am I going about this the wrong way?
Thanks for the help
I am not sure what the "name" option does in the "options" hash. Can you post a link to where you found documentation of that? It looks like you are using the collection select helper properly. What do you mean by "these drop downs would be named unit[]"? It might help if you tell us your end goal of this form as Rails usually just handles stuff for you. Take advantage of its magic.
Also if you are a Rails beginner, highly recommend checking out Ryan Bates' screencasts on complex forms. Here is the link to part 1:
http://railscasts.com/episodes/73-complex-forms-part-1

select_tag is sorting (strangely) [Rails]

I have a select box that looks like this (within a form_for)
<%=f.select(:whatever_id, {"blah"=>0, "blah2"=>1, "blah3"=>2, "blah4"=>3}, {:include_blank => true}) %>
and the output is good, but weird... like this:
<select id="personal_information_whatever_id" name="personal_information[whatever_id]"><option value=""></option>
<option value="1">blah2</option>
<option value="2">blah3</option>
<option value="0">blah</option>
<option value="3">blah4</option></select>
But I want it to go in order... what is happening, and how can I correct it?
Edit: I feel like the answer has to do with this
You can never be guaranteed of any
order with hashes. You could try
.sort() to sort the values in
alphabetical order.
is there anything I can use aside from a hash?
Yes, you should use an array of arrays. The easiest way with your example would be something like this:
<%=f.select(:whatever_id, [["blah", 0], ["blah2", 1], ["blah3", 2], ["blah4", 3]], {:include_blank => true}) %>
This should suffice. Take a look at the docs at api.rubyonrails.com too.
In your model, define your hash (in your case your model is "whatever" and your hash "blahs"):
BLAHS = { "blah"=>0, "blah2"=>1, "blah3"=>2 }
In the select tag where you put your hash, type
Whatever::BLAHS.sort {|a,b| a[1] <=> b[1]}
This creates an array as described in other answers, ordered by the second item (the id/numbers).
After saving, when you pull a Whatever back out of the database and want to show the field blah, do this
Whatever::BLAHS.index whatever.blah
The array of arrays mentioned by others works, but when you want to show a Whatever, how do you show the value of blah in a nice way? I recommend sticking with the hash as it solves this problem.
The problem is that the options parameter is a hash, and hashes have no guaranteed order.
This should work for you
<%= f.select(:whatever_id, ([["blah",0],["blah2",1],["blah3",2]]), {:include_blank => true}) %>
Edit in response to your comment: For collections see collection_select

Resources