Simple form examples with bootstrap
Good reference how to build bootstrap forms with simple_form.
Related cards:
A simple example with a GIN index in Rails for optimizing a ILIKE query
You can improve your LIKE
/ ILIKE
search queries in PostgreSQL by adding a GIN index with an operate class ("opclass") to split the [words into trigr...
Simple Form: Rendering errors without an appropriate attribute
Usually you add errors to :base in ActiveRecord, in case no appropriate attribute could be used to add the error.
Simple Form...
Good real world example for form models / presenters in Rails
We have often felt the pain where our models need to serve too many masters. E.g. we are adding a lot of logic and callbacks for a particular form screen, but then the model becomes a pain in tests, where all those callbacks just get in the way. O...
HTML forms with multiple submit buttons
Most forms have a single submit button that will save the record when pressed.
Sometimes a form needs additional submit buttons like "accept" or "reject". Such buttons usually attempt a state transition while updating the record.
To process a fo...
How to emulate simple classes with plain JavaScript
If you want a class-like construct in JavaScript, you can use the module pattern below. The module pattern gives you basic class concepts like a constructor, private state, public methods.
Since the module pattern only uses basic JavaScript, your...
Advice: Reduce scopes with joins to simple IN-queries
In theory you can take any scope and extend it with additional joins or conditions. We call this chaining scopes.
In practice chaining becomes problematic when scope chains grow more complex. In particular **having JOINs
in your scope will re...
How to deal with 'parent id missing' error in nested forms
tl;dr
- Use form models to handle this problem
- Or soften the validation to
validates_presence_of :parent
Usually you would validate presence of parent object id, like in this example:
class Parent < ActiveRecord::Base
has_many ...
Submit a form with Prototype
For example, to send a form and populate a preview div with the response.
$('content_form').request({
parameters: { 'preview': "1" }, // overrides parameters
onComplete: function(transport){
$('previewContent').update(tra...
Using form models (aka decorators) with Devise
To use a form model with devise, you can simply override #resource_class
in a controller. A typical use case would be the registrations controller, as users will need some fields only on sign-up. Example:
class Frontend::Authentication::R...
Test whether a form field exists with Cucumber and Capybara
The step definition below lets you say:
Then I should see a field "Password"
But I should not see a field "Role"
Here is the step definition:
Then /^I should( not)? see a field "([^"]*)"$/ do |negate, name|
expectation = neg...