Setting Formtastic as Rails 3 default form builder, is it possible? - ruby-on-rails-3

Something like this in application.rb:
# Configure application generators
config.app_generators do |g|
g.form_builder Formtastic::SemanticFormBuilder
end
If I do so I get an error when I try to scaffold a model:
Expected Thor class, got Formtastic::SemanticFormBuilder
Is it possible to set Formtastic as default form builder?
Updated.
I have tried Simple forms and it's really awesome (Thanks to nathanvda). The DSL is almost the same as Formtastic has. The only important difference for me is in customizing button labels. In formtastic it's possible to use resource file (formtastic.yml) to set different labels for the same model and action. Sometimes it's necessary, for example in Devise views. But it costs nothing to switch from formtastic to simple forms even in this case as it's possible to do it in this pretty simple way:
= f.submit t("customized_button_label")
Now about the original question. When I installed simple forms it creates template in lib/templates/haml/scaffold directory which will be used with scaffold. Straightforward.

I am not entirely sure about formtastic, either it does this straight out of the box, so not configuration needed; or not at all.
But what i do know: simple_form does provide scaffolding, even configurable which is totally awesome. The DSL between formtastic and simple_form is close to identical, but with simple_form the level of configuration is much better. You have total control how a form should be scaffolded, you have total control how a single field is turned into html. Pretty awesome.
You can find a quick introduction here.

Related

Adding disable to certain select options in simple_form

A user can chose 5 options. Once they choose one, they can not choose it again. Currently, I simply remove previously used options from the list...so, they can only choose from previously unused ones. The only thing I don't like about this is that as the user is adding a new record to the database, they might wonder why options seem to be missing from the list.
One idea I had was to leave previously used options in the list but cross them out and make them disabled.
Is it possible to disable (and/or add a class) to only certain options in a select? Simple_form seems to have an option_html helper but, didn't see it documented.
Turns out this is quite easy to do. Rails supports it...
<%= f.association :ying, collection: #yangs, :disabled => #used_yangs %>
It works for me only when I pass ids into disabled option. Like this:
= f.association :stuff, disabled: Stuff.where(enabled: false).map(&:id)
Using Rails 4.2.3
It's correct regarding Rails docs. Check :disabled description here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

Model paper clips validations errors are not displayed in simple_form

I am using paper_clip and simple_form gems. Unfortunately, it seems that paper clip validations errors are not displayed in my form.
I have try several types and syntax of paper_clip content_type validations and even they work (the uploaded files types are restricted) no error is displayed in the form.
Has anyone knew know how to fix this?
Here's what I suspect - the paperclip validation is likely being made on an attribute that's not actually an input in simple_form.
You might have something like this in your form:
<%= f.input :picture, :label => "Picture" %>
But paperclip's validation errors aren't going to be put on 'picture' - they'll be put on another attribute, like 'picture_file_name'. Since simple_form has a 'picture' input, and not a 'picture_file_name' input, it doesn't know where to put the errors, so they don't show up.
Take a look at your errors array and confirm what attribute's getting the errors. Then use simple_form's errors helper in your view to put that attribute's error in the appropriate place:
<%= f.error :picture_file_name %>
Unfortunately paperclip provides own format for validation errors storing them in 3 different attributes without linking to base model attribute:
*_file_name, *_file_size, *_content_type
Therefore simple_form or any other form view helper could not map paperclip validation errors with field name.
You can patch paperclip like described here http://dev.mensfeld.pl/2013/05/paperclip-bootstrap-and-simpleform-working-together-on-rails/ and forget about this issue forever

Rails3. Is there any way to separate javascript file for different controllers?

I search around for quite some time but there's are no close solution for this.
For example,
I had generated 2 controllers: Articles, and Calendars.
There have been 2 javascript files also been generated according to these 2 controllers as:
calendars.js and articles.js.
The problem is, if they (2 files) all are included into application.js without any condition. i.e when I open articles/index action, the calendars.js has been imported on page
or when I open calendars/index action (or any action), the articles.js has been imported on page.
What I am looking for is a methodology to separate javascripts file according to the controller that they are belong to?
I also having the same question related to css files.
Thank you and Best regards,
Dat
What you probably want to do is only execute certain pieces of javascript on each controller (action). You can achieve that easily with this gem: https://github.com/intrica/rails_document_ready.
You can add in your index.html.erb file.
For the specific page where you want this js file add in your html file.
<%= javascript_include_tag "articles" %>
I found paloma: https://github.com/kbparagua/paloma
I develop new Rails application for about 2 months with this gem, and it provide quite effective way to manage javascript.

How do you edit Devise error messages?

To be clear, I know how to edit the error messages in config/locales/devise.en.yml but I'm referring to styling these type of error messages:
2 errors prohibited this user from being saved:
Email can't be blank
Password can't be blank
--
All i see is <%= devise_error_messages! %> on the sign-up page, but I don't know how to actually edit the error messages themselves.
In my case the messages appear on the left, and my sign up is centred (which looks odd), I also don't like the red color of the messages and would prefer a different color.
So my question is, how can I style the error message? centre it, and change the color.
Not sure which other controllers or contents to include so as soon as you ask, i'll update the OP with them if need be.
See the sources for the devise_error_messages! method at https://github.com/plataformatec/devise/blob/master/app/helpers/devise_helper.rb.
All the errors are inside
<div id="error_explanation">
so you can use that fact in your CSS. Inside it uses only basic styling: h2 for the header message, ul for the individual errors. See this SO example for #errorExplanation styling, for example: how to beautify validations in rails. Just don't forget to replace #errorExplanation with #error_explanation in the example.
But your best approach would probably still be to rewrite this method or write and use your own, and there apply all the styling you like.
I'd personally recommend displaying errors next to the fields they belong to. See this SO thread, for example, on how to do that: Rails: Errors close to particular fields in forms.
Another improvement would be switching to simple_form for your forms (and getting errors-next-to-fields for free). See, for example, an excellent Railscast on that: http://railscasts.com/episodes/234-simple-form. There's a more recent revised Railscast, but not sure if you're a Pro subscriber there.

Rails form helpers with devise

I am using rails 3.2.3 with devise to sign up and sign in users.
The problem is that devise (I might be wrong here) somehow overrider the interpretation of form helpers where an error in the form occures (for example, the registrations/new form). It puts the labels and the inputs, wich are wrong in some kind of div blocks and ruines the view.
How can I customize that behavior?
UPDATE 1
The views/devise/registrations/new.html.erb looks like this: https://gist.github.com/2585197
And here is the output if errors occur: https://gist.github.com/2585208
You will need to generate views for the model. For example
rails generate devise:views users
This gives you control over the views you get from devise. Check out the documentation here
https://github.com/plataformatec/devise