Thursday, July 10, 2008

A link back to referring page in Rails

So here's the quick problem. You have a long list of records in your rails application, such as posts. You end up using the standard pagination offered in rails, which we all know is slow. Regardless, it's pretty annoying to go down into an individual post, edit it in-place, and then hit the back button, since there's no link back to the previous page.

Well, since it's a pagination, there's not a static page to link a "back to list" link. You could pass in the page, but that's a pain in the butt. It's much easier to do:

<%= link_to "Back up to list", request.env["HTTP_REFERER"] %>

This'll put a link back to the same page in the pagination list that you came from. Saves pains. Ends up reading the HTTP spec on headers is a helpful, in addition to some Rails source.

Ends up that "redirect_to :back" also uses the same trick. You can use that in your controller to just redirect back to whatever method called it.

Tuesday, July 8, 2008

rake, cron, ruby, and scheduling tasks

This post is how to setup a cron job, on *nix server using rake. Why use rake ? well rake allows you access to all your models within your application. So writing a task is like writing any other function, and all that DRY goodness without having to rewrite a thing. A very good write up on Rake here : Rake Tutorial.
Setting up a cron job is simple, here is a write up in the Ubuntu documentation - Cron How To.

So just create a directory in your home, or anywhere , to where you will be placing your shell scripts, and/or logs. You could call rake directly in the crontab, but I prefer to keep it in a script, the more complex it gets later, will be easier to change.

its simple after that
cd / && /usr/bin/rake RAILS_ENV=production raketask1 raketask2

and yes you can chain rake tasks and they will run in that order. Very cool stuff. Another good write up where I found out about chaining - Rake Around the Rosie

Thursday, June 26, 2008

Rails Server Options -- Too early Too Many

Right now there are sooo many options for server stacks, that its pretty overwhelming.

Server Benchmarks - benchmark-passenger-mod_rails-vs-mongrel-vs-thin

ebb vs mod_rails vs mongrel vs thin
apache vs nginx

So what do I use. I think the overall immaturity in some of these offerings are a little scary, and just going with a simple nginx mongrel cluster setup is probably the most standard. Bur reading about the Bitnami stack made me look into other solutions. I will implement this on stack on my vps and have a write up on ease of installation and setup.

Related links :

Nginx "how to" - Fast, Secure and Efficient Web Server
Thin - yet another web server
Bitnami

Friday, May 16, 2008

cookies and ruby on rails

The cookies object looks like a hash, but it isn't really.

# this sets a cookie
cookies[:key]="value"

# this does not retrieve a cookie
value = cookies[:key]

# but this does
value = cookies["key"]

link to documentation ActionController::Cookies

Thursday, May 15, 2008

Acts_as_rated .. Rating Users

acts_as_rated is another solid plugin written by Guy Naor .

The issue I had was I wanted a user to be able to rate another user ???


From what I could tell the issue was that inorder to do this , was that the relationship now become a self referential join rather than a has_one or has_many relationship. What I did was to create a model User - has_one :user_rating and UserRating was added as acts_as_ratable. Any ways if you look within the code, there is a way of adding a statistics table, rather than changing your current model table. Which is all this is really doing and you still have the benefits of all the good stuff Guy wrote.

Also outside of the scope of rating the User model I also made significant changes to fit my use case. Overall the code is clean and easy to follow. The changes I made are for example lets say you wanted to rate actors of movies. But you wanted to allow users to rate an actor more than once based on the movie they performed on. So you would allow a user to rate Brad Pitt for the movie Seven, and Oceans 11.. etc. This was a very simple change requiring to add the ... in this case movie_id to the ratings table, and adding the condition to queries in the rate and rate_by functions.
Also it doesn't save to the decimals for example 7.5. I had to change the precision of the rating_avg columns.

Wednesday, May 7, 2008

class_eval

Very good write up by Neeraj on class_eval stmt in ruby. Overriding, adding methods at runtime. Also make sure to read the comments.

Link to article

Monday, May 5, 2008

Starbox for rating in Ruby on Rails

So graphically I chose the starbox library ( Starbox Link ). Nick Stakenburg I think has done a good job. I am not an expert in js by any means, but its small, and pretty customizable. Also this library is built using prototype and scriptaculous (effects.js), so for rails these are already included. If anyone knows why this isn't a good library please leave a comment. Anyways.. so integration.

* I just realized with latest starbox he has changed the license. Currently I am using an older version with a MIT license. I have created hosting for this project here open-starbox project. Please update with any changes you make to integrate with latest prototype or scriptaculous, if you plan on using.



  • Paths : overlayImages path. When using a nested route , such as /path/id/path/id. I believe this library reads as a relative path .. so passing in "../images/starbox/" .. it tries to retrieve image from path /path/id/images/starbox , and obviously this is not the correct path. This took awhile to figure out. So for now I'm just hard coding the path in the starbox.js file. Besides for this project the path will be static anyways and won't be changing.
  • Parameters : I've had to continue to modify the open starbox library and add to attributes to be passed, the ratedId, and raterId. My use case is where the logged in user, can rate multiple users, but one or multiple, when using the Ajax.request the two id's are needed for the acts_as_rated.   
  • Ajax.request & rails security : this could be a whole blog posting by itself , and I may have to revisit sometime. But when implementing code
    Ajax.request('url_for(path_to_url())')
    within the controller it throws this Exception - ActionController::InvalidAuthenticityToken ??? This security answer link is an answer, but I still don't think this is the best way to do it. Prototype is not passing security token. Update : seems to resolved itself.. very odd.

Note: 05/07/08 the integration of starbox js lib, is correct. acts_as_rated, although is good, can not use to rate User model refer to ruby forge. Look for later post on integration