RSS
 

Archive for the ‘Uncategorized’ Category

Rails 3.1 Redirect back to controller after custom authentication

12 Oct

If, like me, and Ryan Bates, you like doing the important things yourself, so that if there are errors, they’re your errors, chances are you will want to create your Rails authentication from scratch yourself. Ryan of Railscasts has an excellent screencast about this.

I switched from Devise to this in a breeze, and then comes the customization. One of these is the all-important redirecting back to the funnel a user came from after they are done signing in, or up. This can actually be achieved very easily, following some
Devise conventions.

Say I have a CharitiesController, that I want to close to unauthenticated users. In line with Devise, I would do this to force them to authenticate:

class CharitiesController < ApplicationController
  before_filter :authenticate_user!, :except => [:show, :index]

And in my ApplicationController, given i have set the user sign in route to user_login, I can define a return point:

class ApplicationController < ActionController::Base
  def authenticate_user!(return_point = request.url)
    unless user_signed_in?
    set_return_point(return_point)
    redirect_to user_login_path
  end
  def return_point
    session[:return_point] ? session[:return_point] : root_path
  end
end

And then, in my UserSessionsController, I can redirect the user back to the return point:

class UserSessionsController < ApplicationController
  def create
    user = User.find_by_email(params[:email])
    if user and user.authenticate(params[:password])
      session[:user_id] = user.id
      redirect_to return_point, :notice => "You are now signed in"
    end
  end
[...]

Now, whenever a controller calls authenticate_user!, my ApplicationController will save the calling URL before redirecting to the login page, and by fetching return_point from the UserSessionsController, I can redirect them back to that page. I can even override it, such that I can send them wherever I want, should I so desire. Awesome!

 

Rails 3 Magic Migrations

23 Sep

I Always forget what the Magic Migrations (migration generator names that automatically populate the migration with code) in Rails 3 are, so I thought I’d scratch my own itch and post them here for me (okay, and others) to find:

Rails 3 Magic Migrations

 

CreateSchemaName field1:type field2:type
 
AddColumnColumnNameToSchemaName field1:type field2:type
 
RemoveColumnNameFromSchemaName field1:type field2:type

 

 

 
 

Meowgle – Google with kittens. LOTS of kittens

03 Jan

Do you like kittens? I know, who doesn’t, right?

On Meowgle.com you can get your standard Google search, only wrapped in wonderful Kitten pictures. There’s a new kitten image and video every day, so you can start the day with a cute little fluffy kitten :)

Meowgle - Google with kittens

Meowgle - Google with kittens

 
 

Diablo 3 release date info page

03 Jan

So I’m pretty excited about Diablo 3 coming out this year, and decided to create a page dedicated to that most important of dates, the release date. So head on over, and check out my Diablo 3 Release Date page – I think it’s the most stylish one out there :-)

Screenshot

 
 

Rails 3 Cucumber and webrat “undefined method” error fix

03 Dec

So, I recently started using Rails 3, and I am running my test with the awesome Cucumber framework, and Webrat. To set it up in Rails 3, however, I had to apply a little fix, because my
task kept giving me this lovely error

1
undefined method `fill_in' for # (NoMethodError)

Now, the fix for this is very easy – you go to your “env.rb” file under features/support, and change :rails to :rack, like this:

1
2
3
4
Webrat.configure do |config|
  config.mode = :rack
  config.open_error_files = false # Set to true if you want error pages to pop up in the browser
end

And presto! It works!

 
 

My LAIR IEEE published article

17 Aug

Okay – this will be the last repost of stuff from my old blog:

I had an article “LAIR: Language of Automatically Inferred Redaction” published in the IEEE paper ICSC, and as a result I, together with my project partner Steffen Hedegaard were invited (and went) to speak at the conference in Berkely, San Fransisco. Fun times. Here’s the article.

And the brag link: LAIR at IEEE