I’ve been working on a few new projects lately and wanted to share a few little tips I’ve started doing.
Conditional Buttons for Shared Forms
Something that I like to do is use a shared form for both my edit and new views. Doing this however means I need to be friendly to the user interface and make sure the submit button is properly instructional. So for example if I have a Customer model with an instance variable @customer my button would look like this:
<%= f.submit((@customer.new_record? ? "Create" : "Update") + " Customer") %>
With this I’m checking to see if the @customer instance variable belongs to a new record and if so it’s outputs Create, otherwise it’s an Update button.
Cleaner RedCloth Helper
There is the built-in textile helper that comes with Rails, it’s carage return rendering is a bit lame so most people typically upgrade their RedCloth gem and using something like:
<%= RedCloth.new("My copy that requires formatting").to_html =>
I prefer to instead create an application level helper for redcloth (some people might instead overwrite the textile helper here, but I find that can be confusing to some people looking at your code for the first time). My helper looks like this:
def redcloth(str) RedCloth.new(str).to_html end
So now when I want to redcloth something I just call:
<%= redcloth("My copy that requires formatting") %>
Simple Little Permalink
When I have a simple object that I want to create more user-friendly URLs for, I’ll create a basic permalink. In the instance of the same Customer model from tip 1 above, I like to use the customer name as the permalink. To do this of course the name has to be unique so make sure you are validating it’s uniquiness above all. Then I create a permalink column in the database table and write something like this in my model.
def name=(value) write_attribute :name, value write_attribute :permalink, value.gsub(/\s/, "-").gsub(/[^\w-]/, '').downcase end def to_param permalink end
This uses the value of the name that is entered, clears it of puncuation, replaces spaces with hyphens and drops the casing. The first line makes sure it still remembers it needs to write the value itself to the name column in the model.
Now it’s still important that you confirm that the permalink is unique too, but I’ll let you do that on your own.
So that’s it – I hope you guys find it useful.