Sal (salt) is something you add to almost every meal. I add sal to my Rails projects.
I found I always add a particular set of helpers, initializers and generators. Better if they're distributed as a Rails engine.
Add to your Rails app Gemfile:
gem 'sal-engine'
And run bundle
Will render:
<div class="alert alert-success">anything</div>
…if flash[:notice]
contains anything.
Will render:
<div class="alert alert-warning">anything</div>
…if flash[:alert]
contains anything.
When providing both, notice
will prevail.
Very handy for navigation menus:
<%= menu_item 'first' do %>First option<% end %>
<%= menu_item 'second', tag: :div do %><%= link_to root_path, 'Second option' %><% end %>
<%= menu_item 'third', class: 'nav-link' do %>Third option<% end %>
Renders:
<li class="active">First option<li>
<div><a href="/">Second option</a><div>
<li class="nav-link">Third option</li>
Parameters:
menu_item(item, options)
- item: item name
- options: a has of options such as :class, etc… An additional option :tag is provided in case you don't want to use LI as default
Will indicate if any model in your form has an error. It supports multiple models
in case you're combining two entities in the same form (i.e. using fields_for
):
<%= errors_item @user %>
Renders:
<div class="alert alert-danger">Please correct the highlighted fields.</div>
Parameters:
errors_item(model[, model2…])
- model: an active record model, or tableless
- model2: you can provide more than one
The string Please correct the highlighted fields.
is localized as errors_bar_message
into English, French, German, Italian, Portuguese and Spanish. You can override
this in your app if you need to, editing your locale yml files.
I like to put the error messages right after the field in the form. sal provides an initializer that will patch ActionView::Base to wrap the field with the extra markup and the error message.
Given a model with a field:
<%= text_field_tag :title %>
that renders this simple view:
<input type="text" name="title">
Then it will add this extra HTML on an error:
<div class="has-error">
<input type="text" name="title">
<span class="help-block">✖ can't be blank</span>
</div>
It plays nice with Bootstrap forms. You won't have to touch any CSS.
- Plain text fields:
<div class="form-group">
<%= f.label :password, 'Password' %>
<%= f.password_field :password, :class => 'form-control' %>
</div>
- Checkboxes. Use this code for proper margin and alignment:
<div class="form-group checkbox">
<%= f.label :conditions, "#{f.check_box(:conditions)}I accept the terms and conditions".html_safe, :class => 'checkbox' %>
</div>
Rails generators are very useful in the initial stages of development to create your admin interfaces. In particular:
- I use bootstrap classes for layout
- I don't use the show action/view
- I don't want to autogenerate helper files for every model
To launch this generator:
bundle exec rails generate scaffold MyModel name:string -p --skip-test-framework
It fits my purposes, which may be different from yours. You might like the idea though: then fork it and call it peppermint, curry or whatever you prefer!