Simple Form - Radio Buttons - ruby-on-rails

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

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>

In simple_form, can't submit boolean as radio_button if the field is disabled

I am using simple_form 2.0. I have a Boolean field 'stock' which I am trying to submit as radio buttons.
<%= f.input :stock , :as => :radio_buttons, :collection => [['Purchase Indent', false],
['Stock', true]], label:"Shipments From" , :disabled => true%>
The stock is marked as false before rendering the form.
Once I submit the form the stock itself is missing from the parameter and I get this error.
Because I am validating stock's inclusion.
validates_inclusion_of :stock, :in => [true, false]
It works fine if i don't disable the field. But I don't want user to be able to change it.
Please help.
Update
The reason is that, the disabled fields are never sent. http://www.w3.org/TR/html401/interact/forms.html#h-17.12
Seems like making it read-only will help.
https://github.com/plataformatec/simple_form/pull/367
But, the news is radio buttons can't be made read only.
Why can't radio buttons be "readonly"?
One option is to separate the buttons and only disable the unselected option:
<%= f.input :stock , :as => :radio_buttons, collection: [['Purchase Indent', false]], label:"Shipments From" %>
<%= f.input :stock , :as => :radio_buttons, collection: [['Stock', true]], label:"" , :disabled => true %>
Another option would be to add a hidden input with the desired value.
Remember not to trust user submitted data.
I don't think you should build it like this, because a hacker can just change the HTML / submit an artificial request even if you disable the form elements. Hidden form elements don't fix this, as anyone with a dom explorer can change the values. Of course, if your model checks and rejects this kind of thing it's not such a big problem.
So to fix the particular problem, just do the visuals as you have already, and re-insert the expected value in your controller's update or create action.
For more info there's lots online e.g. owasp, but I liked the book "How to break web software" from a few years back by some guys at Google.

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)

Wrong number of arguments (2 for 4) using simple_form checkboxes

I'm a little confused by the read me for simple_form in regards to a collection of check boxes. Namely, what else am I expected to put on the line besides my options. The readme has :first, :last but doesn't really explain what purpose those words server.
Below are two different collections of checkboxes I'd like to have. I should also mention, each checkbox relates to a boolean field in the DB and they are labeled the same as the column names.
<%= f.collection_check_boxes :options, [[false, 'App'], [false, 'DB'], [false, 'WCF']] %>
<%= f.collection_check_boxes :options, [[false, 'Com1'], [false, 'Com2'], [false, 'BofA']] %>
I forgot to mention, the error I'm getting when I try to load this page is
wrong number of arguments (2 for 4)
This error simply mean that your <%= f.collection_checkboxes %> takes 2 arguments but your giving it 4. Remember this for future reference as this mistake can be easily made.
Right from this example below:
form_for #user do |f|
f.collection_check_boxes(
:options, [[true, 'Yes'] ,[false, 'No']], :first, :last
) do |b|
b.label { b.check_box + b.text }
end
end
The rubydoc.info website states the following:
It is also possible to give a block that should generate the check box + label. To wrap the check box with the label, for instance as above:
So from my understanding of this the :first and :last represent the labels as it were. So that you'd have two check boxes. That are either true or false but then outside that option array you are providing the label those instances of the class. So lets take this example. That is providing the label the instances :first and :last of the class User. Hope this clarifies things. The link is provided below:
Ruby Doc- SimpleForm collection_check_boxes

formtastic check_boxes using collection not saving checked items in edit form

I know this was supposed to be fixed in Formtastic 2.0 but for some reason, this isn't working for me.
<%= f.input :gender, :as => :check_boxes, :collection => ["Male", "Female", "Unisex"] %>
Whenever I go back and check it, the items is always unchecked again. It never appears as selected. I know Justin French commented about it here, but I am not sure what it means or how that can help me fix the problem.
I am saving gender as a string in the User model if it makes a difference.
I am also using it with formtastic-bootstrap 1.1.1
Thank you!

Resources