Showing posts with label Rails. Show all posts
Showing posts with label Rails. Show all posts

Sep 6, 2011

Watir on Rails

I have been using Selenium + Capybara for a week now. Somehow, the clicking on Selenium is super unstable. The test cases occasionally fail.

It is really strange because Watir also uses selenium webdriver. But anyway the problem has gone now.

I have decided to give Watir a try. Anyway, Watir does not have an easy way to integrate with Rails 3.

Therefore, I have decided to build a gem for that.

And I proudly present you my first gem:

watir-webdriver-rails

I'm so friggin' happy :D :D :D

The gem is hosted on Rubygems.org. Therefore, you can install gem through the normal way

Apr 26, 2011

Delayed Job cheatsheet

To add email sending to background job, use:


# NotificationMailer.deliver_welcome_user(@user)
# We do not use the above line anymore
NotificationMailer.send_later(:deliver_welcome_user, @user)


Or you can use a more customizable version:



# put this in lib/delayed_notification.rb
class DelayedNotification
attr_accessor :user_id
def initialize(user_id)
self.user_id = user_id
end

def perform
NotificationMailer.deliver_welcome_user(User.find(@user_id))
end
end

# Add this where you were sending the mail earlier
Delayed::Job.enqueue DelayedNotification.new(@user.id)


Ref: http://8raystech.com/2009/2/28/background-job-processing-in-rails-with-delayed_job

Apr 23, 2011

How to get Rails version

You can refer to


Rails::VERSION::STRING


It will return a version as string, e.g., 3.0.3

Apr 20, 2011

Run background process with Capistrano

To run a background process with Capistrano is so difficult. I have tried few different ways and I found the working one.

It turns out that you need to use a command, nohup together with &, which is put in a shell script file.

Then in Capistrano file (deploy.rb), you'll need to use run the shell script with >/dev/null 2>&1.

And that's it.

-------

I have tried use :pty=>true option on the Capistrano's run command. It produces a very strange behavior. Sometimes the background process is started and sometimes it is not...

And without >/dev/null 2>&1, there will be an output, which blocks Capistrano from proceeding forward...

Jan 11, 2010

InvalidAuthenticityToken

A controller always throw InvalidAuthencityToken, if it receives HTTP Post from a form which is not created from the controller itself.

It is the Rails protection mechanism.

Therefore, to solve it, we have to turn it off.

In the controller, just add this line:

protect_from_forgery :only => [:create, :update, :destroy]


It specifies that the protection mechanism only applies to those three actions (create, update, and destroy).

For other actions, they can receives a third-party HTTP Post without throwing InvalidAuthencityToken.

Debugging Rails

I've just had a painful experience: Rails does not accept an HTTP request from facebook canvas, and all it returns is HTTP 422 status, which practically means nothing.

After googling for a while, I have found a way to debug a rails server. Simply, runs

ruby script/server


and that is it. You will see debugging message from Rails on the terminal.

And another good thing about it is that you do not need to restart the server everytime you make a change.