RSS
 

Archive for the ‘Ruby on Rails 3’ 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!

 

Carrierwave with Rails 3.1 upgrade

12 Oct

So I had some trouble with Activesupport and CarrierWave when upgrading to Rails 3.1, so I’m putting my solution here, hopefully for others to enjoy. I got this error:

Bundler could not find compatible versions for gem “activesupport”:
In Gemfile:
rails (= 3.1.1) depends on
activesupport (= 3.1.1)

carrierwave depends on
activesupport (3.0.10)

And the solutions was to do the bundle update command

bundle update

Or, if you like me are using rvm

rvmsudo bundle update

And now everything works just perfect ^^

 

The best Ruby on Rails book

13 Mar

I have found a book that is really great. The best beginners’ book for Ruby on Rails 3 to me: The Rails 3 Tutorial book

I’m always learning new programming languages for projects (just have a look at the books ive read so far), and though they’re often very similar, a good book is essential to me in grasping new languages quickly.  For Rails, I started with Agile Web Development with Rails by the Pragmatic guys, but found that it was way too problem-specific and not giving enough real rails knowledge. So i went back to the official guides, which are very, very, very good.

Now, however, I have found what I was looking for: The Rails 3 Tutorial book

And to sweeten the deal, you can read it online for free (though I bought and printed it – you just cant beat the reusability of a book that you’ve underlined yourself) and there’s even a bunch of screencasts to further your education after reading the book.

Thank you Michael Hartl