Tuesday, June 19, 2007

A small Rails cheatsheet

We constantly forget the syntax to assign CSS classes to various Rails helpers. The general idea is that most helpers take two hashes (that's the part inside curly brackets) - the first hash has specific options for the helper, the second hash gets passed on to the resulting HTML code. Thus, pay special attention to the curly brackets.

Here's a quick cheat sheet:

A simple link:

<%= link_to "Link name", { :controller => 'controller_name',
:action => 'action_name' },
:class => 'someclass' %>

A more complicated link:

<%= link_to "Link name", { :controller => 'controller_name',
:action => 'action_name' },
{ :class => 'someclass',
:id => 'css-id',
:confirm => "Are you sure?"} %>

An AJAX link:

<%= link_to_remote "Link name", {:update => "div_to_update",
:complete => "js_on_complete",
:url => { :action => 'action_name', :id => 'some_param' }},
{:class=>"someclass"} %>

A simple form tag:

<% form_tag ({:controller => "controller_name",
:action => "action_name"},
:class => "someclass") do %>
insert code
<% end %>

An AJAX form tag:

<% form_remote_tag :update => "div_to_update",
:complete => "js_on_complete",
:url => { :action => 'action_name', :id => 'some_param' },
:html => { :class=>"someclass" } do %>
some content
<% end %>

A simple text field:
<%= text_field "object", "field", :class=>"someclass" %>

Submit tag:
<%= submit_tag "Submit", :class=>"someclass" %>

Text area:
<%= text_area "object", "field", :class=>"someclass" %>

3 Comments:

Blogger Mark Molckovsky said...

Thanks Alex! How'd you know this is exactly what I wanted. :)

June 19, 2007 3:10 PM  
Blogger Alex Curelea said...

I'm psychic!

June 19, 2007 3:19 PM  
Blogger Doug Howie said...

Very cool - succinct but very powerful. Spent most of the afternoon looking for just this sort of information....

October 29, 2007 1:55 AM  

Post a Comment

<< Home