Simple Form Select with Rails Bootstrap - ruby-on-rails

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>

Related

How do I create a multiple item select list in Rails from database

This is my code - it's working fine but I want to be able to select multiple Organisations, while this select only allows me to choose one.
<%= f.collection_select :id,
Organisation.order(:Company_Name),
:id,
:Company_Name,
options = {include_blank: "Select an Organisation"},
html_options = {:onchange => 'broadcast_dropdown_change(document.getElementById("broadcast_country_id"), document.getElementById("broadcast_organisation_id"))'} %>
Please help me resolve this, along with any advice on how I can improve this code.
Thank you.
I think you just need to add the html option multiple: true :) Hopefully that'll do it, nice and easily!
If not, you'll need to ensure the permitted parameters match up to what's getting passed through - likely a JSON encoded array of options - and parse / save them as appropriate in the database.
Couple more examples available in the duplicate I've linked in the comment to your post. Let me know how you get on and I'll provide a little more info if need be!
Update based on your comment, here's an example:
<%= f.collection_select :id,
Organisation.order(:Company_Name),
:id,
:Company_Name,
{ include_blank: "Select an Organisation" },
onchange: 'broadcast_dropdown_change(document.getElementById("broadcas‌​t_country_id"), document.getElementById("broadcast_organisation_id")',
multiple: true %>
Couple of notes here:
the last two arguments this method takes are for options and html_options; the include_blank: ... is in the former, and the final two arguments the latter
you don't need to assign these hashes to variables. The first hash needs the brackets to distinguish it from the second, though the final hash is implicit so doesn't require the brackets (though you can use them, if you find it easier to read, i.e.)
{ onchange: 'broadcast_dropdown_change(document.getElementById("broadcas‌​t_country_id"), document.getElementById("broadcast_organisation_id")',
multiple: true }
have a quick look at how you're naming your variables - it's worth spending the time to format them correctly - for example, :Company_Name should always be lower snake case :company_name
Hope that helps - again, give me a shout and let me know how you get on :)

Simple Form - Radio Buttons

Please can someone help me with using Simple Form in my Rails 4 app.
I have a model with an attribute for a 'project_image'. I ask whether a bespoke image should be included and want a yes or no answer.
<%= f.collection_radio_buttons :project_image, label: 'Would you like add an image to your project pitch?', :options => [[true, 'Yes'], [false, 'No']], :first, :last, :style =>"display:inline", :default => true %>
I keep getting errors with this line of code. They best information coming out of my code editor is that there is a syntax error somewhere (syntax error, unexpected ',', expecting =>). I've tried removing all the commas, one by one and replacing with => and am not getting anywhere.
I tried following this answer and didn't find any success.
Simple form radio button
Help would be very much appreciated. Thank you

How to implement multi select in a independent table in Rails?

My problem is that I have, for example, Product, Category and ProductCategory.
ProductCategory makes possible for a Product have several Categories
I would like to implement this using Select2 (http://ivaynberg.github.io/select2/) using the select2-rails gem (https://github.com/argerim/select2-rails)
I already know how to relate the models but I can't figure out how to implement the Select2 specific code.
EDIT:
Now I see that my problem was not much about select2, so I added this comment and changed the title hoping that it can help somebody else
Now I see that my problems were not about select2 but to do a multi select.
The code in the _form.html.erb that make it work is this one:
<%= f.label :category_id %>
<%= f.collection_select :category_ids, Category.order(:name), :id, :name, {:selected => #product.category_ids, :include_blank => true}, {:class => 'col-xs-12 col-md-7 padding_15', :multiple => true} %>
I also included :category_ids in the attr_accessible on models/product.rb
And the select2 specific, I included in a .js file
$(document).ready(function() {
$('#product_category_ids').select2();
});
I'm including these links as they helped me but pay attention on the differences depending on the Ruby/Rails versions
http://www.dzone.com/snippets/using-mutiple-collection
http://www.alethe.com/brad/2009/10/multiple-select-list-in-rails/
Just to let you know, unexpectedly, if this collection_select is the last line in my form, some form fields are disabled although there is nothing that states this in the source. Changing the order this problem doesn't exist.
I also don't know why the look is a little different than the other fields (I'm using Bootstrap 3)

select(object,...) how to get the html options for this type of drop down menu

I'm using this drop down menu for my associations:
<%= select("price", "product_id", Product.all.collect {|p| [ p.name, p.id ] }, {},{ :class=>'chzn-select'}) %>
I was able to get he :class but how do i get other options such as :placeholder and :size?
Help would be highly appreciative, i cant find examples using select only after research.
P.S. Is there a better way to handle this? I am trying to make it more human friendly.
By placeholder do you mean prompt? In that case you'd put it in that first, empty hash, the 3rd argument, for helper options. The fourth is for any and all html tag options.
Edit: By placeholder do you mean HTML5's placeholder? Does that even apply to <select> tags? In any case, the same options hash where you specified the class would the place for any valid HTML options.
There are some example in the docs
For "better" options in this case #collection_select would be applicable, e.g.:
collection_select(:price, :product_id, Product.all, :id, :name, {:prompt => true}, :class => 'chzn-select')
"Better" is your call when it comes to Rails standard form helpers though. They tend to be rather inconsistent and have about umpteen ways of expressing the same thing, so just do what you're most comfortable with.

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

Resources