Rails and Stuff at Abel killed Cain

Ruby on Rails and other Webdevelopment

Archive for the ‘ Ruby on Rails ’ Category

Ruby on Rails provides a nice way to display error_messages; just by adding form.error_messages you get a nice overview of all your errors.

But i prefer to have it in different ways, so i 1) dont want the on_error_message-output to be a div element, 2) further i want to give each input field having bad content to be marked – by adding an error-class to that specific element for example.

Since i had to figure it out, here are the results for changing both.

1) different element instead of a div at on_error_message?
[More]

Rails logging methods are wonderful for development.

Indeed, they often suck when you want to inspect objects or cascaded objects. So here is a codesnipped that helps you inspect any kind of any object in rails, deeply and detailed.

as a new lib-file or, just as i do, in application_controller.rb, add the follogwing code:

[More]

Here is a small collection of Tips and Tricks for Ruby in association to Rails.

I will paste some irb-logs, displaying all results.
1. Format decimals or text quickly
[More]

This is more like a rather small hint, describing how to make tables sortable in rails.

Just like ryan bates shows in his rails casts, there is a similar way to sort tables, instead of lists.

Here the example-code how to make the tables sortable:
[More]

I wanted to have a rather simple pagination without next and previous buttons (yah, the post title told you, eh?).

However, did not find any option to deal with it. But Will_Paginate provides a nice way to overwrite the link renderer in order to have full customization.

So i created the following:
[More]

I had the following problem to solve using rails:

  1. Having 2 Subdomains, www and clients
  2. a client always has a path-prefix to use google analytics for each of this subdirs. so i had the following urls:
    www.domain.tld/foo.html  and clients.domain.tld/3kunsdf7w/foo.html
  3. I did not want to have a code on www.
  4. i did not want to alter all tempaltes with new paramters and differen routes

Solving this, it was ok for me to duplicate all routes with a client_  prefix using textmate, so i took onl 4 seconds.. but what next, how to tell rails to use the clients_xy_path when i call xy_path?

[More]

Routes in Rails are great. No doubt!

Anyways, it fails at URLs like “category/articleid-a-few-words-headline-stub.html” etc. if you try the following:

1
map.article '/:article-:stub.html', :controller=>'article', :action=>'show'

therefore here a quick workaround to get nice dashes in routes with article-id and so on.. [More]

Rails Internationalization (I18n) API is a great way to start with multilanguage pages.

However, i found it quite horrible to specify my translations inside if the corresponding #lang.yml/.rb files etc.

After searching a bit, i found globalize2 – a nice plugin that creates localization-models for each model you want to translate.

quite all you have to add to your code is

1
2
3
class Post < ActiveRecord::Base
translates :title, :text  #this over here
end

Quite nice, but: a new sql-request for each model you want to translate – that sucks!
[More]

Guess you never had and never will have this issue!

Anyways, a note for me about rails complaining about a too long cache key like by caching a list fo products in an eshop.

so instead of e.g. the following pseudo fragment-cache code

1
2
- cache [ @products, "things_i_dont_own"] do
    = render :partial...

try, only if its a sequential list:

1
2
- cache [[ @products.first, @products.last], "things_i_dont_own"] do
    = render..

if its a random created list, maybe you want to map all ids, like

1
@products.map{|x| x.id}

if @products is a big list, try to create indiviual hashes instead and use sweepers.

regards!

Ever wondered how to e.g. send both, low and high-priority emails from the same server/app using ar_mailer in Ruby on Rails?

Propably not, since all your emails are low! But just try to imagine websites you could never get that public by yourself, having high priority mails. E.G. Passwordrecoverymail might be HP than a “new guestbook entry” and so on.

ar_mailer does not provide a solution for that problem yet, so here is my little patch.

[More]