Using Multiple collection_select elements on a form with multiple models - ruby-on-rails

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

Related

Simple Form Select with Rails Bootstrap

I realise most questions relating to using Simple Form (of this nature at least) go unanswered. I'm really struggling to understand the basics. The documentation is premised on too much knowledge for me to figure it out.
This is the overview from the simple form documentation that I'm trying to understand. A worked example would really help me to learn.
#attribute_name, #column, #html_classes, #input_html_classes, #input_html_options, #input_type, #options, #reflection
I'd really appreciate some help with using select options in Simple Form.
Below is the extract from the Simple Form overview.
f.input :age, collection: 18..60, prompt: "Select your age"
I'm trying to use that as an example (along with the country priorities shown in the Simple Form overview example) to make the options between the elements in my array work. Can anyone see what I've done wrong. My attempt below (and many others) have not worked. I'm stuck.
<div class="question-project">
<%= f.input :expense_type, collection: [Incidental, Less than $500, More than $500 ], prompt: "Are the expenses:"%></div>
Thank you
The array needs to have strings, like so:
<%= f.input :expense_type, collection: ["Incidental", "Less than $500", "More than $500" ], prompt: "Are the expenses:"%></div>

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

Read from params[] in Rails

I use:
<%= select( "payment", "id", { "Visa" => "1", "Mastercard" => "2"}) %>
and I get this in HTML
<select id="payment_id" name="payment[id]"><option value="2">Mastercard</option>
<option value="1">Visa</option></select>
now how can I read the payment[id] with params[], if I use params[payment[id]] I get an error.
I suppose is better to have
params[:payment][:id]
Params is a hash and can be contain some other hash.
This one had me stumbled for a couple of hours when I first started with ruby/rails. In your controller and views you can access the payment's id with either:
params[:payment][:id]
or...
params['payment']['id']
Many people prefer using symbols (:symbol) over strings because of memory usage, no matter how small the gains...
params[:payment][:id] and params[:payment][:id] is the same on surface ,
but in fact , in ruby ,you can't access the payment's id with params[:payment][:id].
because rails has changed the usage of it.

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>

Resources