Tuesday, January 22, 2008

Sorting an array

I was following this tutorial that shows you how to sort an array. My action looked like this:

def index
@articles = NodeType.find(1).nodes + NodeType.find(2).nodes
@articles.sort_by { |article| article.updated_at }
end

But that didn't work! It wasn't sorting at all.. at least as far as I could tell. Strange, because this worked:

def index
@articles = NodeType.find(1).nodes + NodeType.find(2).nodes
@articles = @articles.sort_by { |article| article.updated_at }
end

It seems you have to assign it to an object first, before returning it.

Wednesday, January 16, 2008

redirect_back_or_default in Rails 2.0

Since they removed redirect_back_or_default in Rails 2.0, and I still had a need to do something like that, I made method you can put under the private portion of your ApplicationController:

def redirect_back_or(path)
redirect_to :back
rescue ActionController::RedirectBackError
redirect_to path
end

and you use it just like the redirect_back_or_default method.

Thursday, January 3, 2008

TemplateError, Markaby, HostingRails

Markaby in HostingRails causes some TemplateError. To fix it, in vendor\plugins\markaby\lib\markaby\rails.rb, replace

compile_and_render_template(template_extension, template, file_path, local_assigns)

with

compile_and_render_template(@@template_handlers[template_extension.to_sym], template, file_path, local_assigns)

source: comments section of a posting in the RoR blog.