simple_form, manually place the field and the error label - ruby-on-rails

In a simple_form (with bootstrap integration), i have a checkbox with a short text.
The text is 'I've red'.
In the case the user doesn't check the checkbox, the validation error is:
'You have to confirm that you've read'.
As you can see the look isn't very good, since i would like at least the checkbox to be alligned with the 'I've read' label and not with the error message.
This is the code:
<%= f.input :number_of_items, :label => "I've read" %>
So i tried the inline label, that is:
<%= f.input :number_of_items, :label => false, :inline_label => "I've read" %>
but now the result is as follows.
That is: the checkbox is inline with the text, and this is correct... but as you can see the error is rendered at the turn of the lines!!!
I'm looking for a solution to this specific issue.
Then I'm looking for a further customization possibility that is a way to render independently a control and its error message.
This would help me achieve a better, ad-hoc rendering where none of the defaults fits well.

Related

Simple Form For - Rails - f.association options

I have the following field in my form:
<%= f.association :account_manager, collection: Person.appear_on_register_page, :value_method => :id, :label_method => :full_name, :required => true, prompt: "Please Select", label: label %>
This gives the user a textbox, which when clicked on gives a dropdown with every record in the collection. When typing in the text box, this dropdown is narrowed to only match what is in the text box.
I've been asked to remove the dropdown until a user has started entering text - for example when first clicking on the text box no drop down appears until the first 3 letters have been entered - then all records matching those three letters appear.
I've looked through their github and can't find any options for this.. Any help is appreciated
Hope you are looking for a auto complete text box. You can achieve this using Jquery UI.
This is a good gem you can go with.
This is one of the good tutorial for that. Thanks

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

Make simple_form honor my blank/empty label

Using simple_form v2.1.0 in a Rails 3.2.13 app. I have a simple_form where I want to have a checkbox show up with an inline label, and I want to have an "empty" normal label. This way, the label's container should remain in place and the checkbox will be properly positioned with the other form elements.
If I set :label => "" or :label => " " or :label => nil or don't provide a :label at all, I end up with the field name as the label.
If I set :label => false, the label and containing elements aren't rendered at all (so the checkbox appears at the far left of the page).
If I set :label => " ", I end up with the literal text , as expected.
It seems there is no way to tell simple form to leave the label there, just don't put any text in its container.
Here is an example form where I'm trying to setup an empty label for a checkbox with an inline label.
<%= simple_form_for #model ... %>
<%= f.input :sticky, :label => "", :inline_label => "Sticky?", :as => :boolean ... %>
<% end %>
But simple_form thinks it's smarter than me and defaults the label. I wish it only did that if I hadn't specifically supplied a label!
Try setting :label value to false.
UPDATE: Since SimpleForm is supposed to rely on ActionView to render the elements, you can use this hackish solution: set the label in your locale file to empty string, like this
en:
activerecord:
attributes:
model_name_underscored:
sticky: ' '
Have you tried removing the :label attribute in your input completely and instead using form.label for the empty label.
http://apidock.com/rails/ActionView/Helpers/FormHelper/label
Instead of using an empty label you could position your check box with CSS.
Was having similar troubles. In your form defaults just do this:
defaults: { :label => 'blanklabel', label_html: { class: 'blanklabel'}
Then in your CSS, just set the label text color to white:
.blanklabel { color: white; }

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.

Adding description paragraph to ActiveAdmin /new Page

I have a page set up on ActiveAdmin and when the user clicks the "Create One" button, I want to add a short textbox on the /new page (either at the top or part of the actual form, whatever's easier) explaining what needs to be put into the form.
How can I do this?
Active Admin gives complete control over the output of the form by creating a thin DSL on top of the fabulous DSL created by Formtastic (http://github.com/justinfrench/formtastic).
And you can add :hint for each form input like this:
form do |f|
f.inputs 'Details' do
f.input :title, :required => true, :hint => "This field should be filled in"
end
f.inputs 'Advanced' do
f.input :keywords, :hint => "Example: ruby, rails, active-admin"
...
end
end
Take a look Formtastic documentation, there is lot of capabilities...
You can simply add new description column in your model or you can use active admin comments here is comments description for active admin.
Hope it would help you.

Resources