SimpleForm remove wrapper divs - ruby-on-rails

I'm using SimpleForm in my Ruby on Rails app. To make my input look good with my current css I need to disable wrapper every time for the input:
f.input :email, wrapper: false, input_html: { class: 'input' }
How can I remove this wrapper globally?

If by "globally" you mean all forms in application, then I don't think it can be done through standard configuration in config/initializers/simple_form.rb. However, you can easily disable wrappers in a form by setting defaults for all inputs in it:
<%= simple_form_for #user, defaults: { wrapper: false } do |f| %>
Please remember that wrappers in simple_form are highly configurable and controlling classes for them is possible with options available in initializer.

Related

Adding a css class to an input if it has an error using devise

I'm at a point in my app where if the users encounters an error in the form I'd like to change the appearance of a an input. From what I see, devise doesn't add any classes to the input per se if this is the case. Is there any built-in way to achieve this without doing it manually by checking for errors: resource.errors[:username].any?
<%= f.input :username, wrapper: false, input_html: { class: "#{'error' if resource.errors[:username].any? }" } %>
Note: I pass in wrapper: false, which eliminates the addition of fields_with_errors class appended to the wrapper.

How can I create wrapper for the types in the simple_form gem?

I want to create a wrapper for my app, but html for different types should be different. So, I can write just
= simple_for_for #task, wrapper: :mine_special do |f|
= f.input :first_attr, type: :sring
= f.input :second_attr, type: :boolean
so, my initializer will contain definitions for both string & boolean types:
config.wrappers :mine_special, types: [:boolean] do |b|
# configs here for check box
end
config.wrappers :mine_special, types: [:string] do |b|
# configs here for text field
end
How can I do that?
In short
The best solution I can think of is just naming your wrappers differently:
config.wrappers :mine_special_boolean
config.wrappers :mine_special_string
It follows simple_form semantics nicely and also eliminates the need to remember to pass type option.
Explanation
By the time you are passing wrapper: :mine_special option in the template, simple form initializers code has already been run, so you cannot simply insert an if statement in wrappers block. When you call the wrapper from template, it merges your options with the ones you defined in initializers.
I can imagine a way to override some methods in SimpleForm::FormBuilder to route your wrapper calls based on type option you pass in, but at that time I'll probably be wondering if it's worth the complexity.

How do I get simple_form and bootstrap to work together?

I'm using simple_form to generate my forms in my Rails application. I'm wanting to use bootstrap with these forms. So I've installed the following gems and added them to my gemfile... (1)simple_form, (2)twitter-bootstrap-rails. Finally, per instructions on the simple_form git page I ran, rails generate simple_form:install --bootstrap.
After running bundle install everything is installed and the server runs. The forms however are still not displaying correctly as they would with the class "form-control".
Form
= simple_form_for #user, html: { multipart: :true, class: "form-horizontal" } do |f|
= f.input :headshot_image, as: :file
= f.input :remote_headshot_image_url, placeholder: "Image Url"
= f.input :first_name, required: true
= f.input :middle_name
= f.input :last_name, required: true
I am able to get it to "work" by two different methods (neither of which I believe are correct). First I can add , class: "form-control" to each input, or I can add to the simple_form_for :defaults => { :input_html => { :class => "form-control" } }. The former which kinda defeats the purpose I believe, and the latter works, but it also applies the class to the file input which is not ideal.
Is there a setup step that I missed? Or have I done something incorrect. If I need to provide more information or left something out please let me know. Thanks!
Probably you forgot about adding bootstrap assets to application.css file
simple_form Readme has following line:
You have to be sure that you added a copy of the Bootstrap assets on your application.

simple_form clickable value using link_to

hopefully a simple question.
Is it possible to make the value of a simple_form field a link_to?
I have tried
= f.input link_to(:project_id,project_path(#project_action_plan.project_id)), readonly: true
and also
= f.input :project_id, :input_html => {:value => link_to #project_action_plan.project_id, project_path(#project_action_plan.project_id)}, readonly: true
But am not getting the desired result.
Google only really brings up this stackoverflow result which was more about then using ajax etc and the simple_form Github doesn't mention anything. I am thinking it may not be possible and that I will have to make the label clickable.
The way the helper methods work is very specific. If you want to write your own custom helper then it's possible to create your own arbitrary formatter methods by creating a subclass of FormBuilder and using that when rendering your forms.
More information on this subject is available in the documentation on Form Helpers.

Ruby on Rails looking for value from key in devise gem

I'm new to ruby on rails, and I installed devise on my application. I was wondering where I can customize the values if a value is given in views/devise/registrations/new.html.erb
for example:
<%= f.input :password_confirmation, :required => true %>
this gives out an input and a label, the label is * Password confirmation. How do I customize this label and the input field?
Thanks
<%= f.input :password_confirmation, :required => true, :label => 'something', :class => 'something' %>
Define the something class in CSS and you'll be fine.
And of course, generate the partials via copying from devise directly or using its generators. I usually copy them from devise repo since I am not that much fan of generators, sometimes they generate files I don't even need or want.
rails generate devise:views users
You will need to generate the partials first. After that you can use those generated files to override the default ones. Also, it might be good to build the authentication from scratch the first time in order to better understand the process.
http://railscasts.com/episodes/250-authentication-from-scratch

Resources