Showing posts with label webdev. Show all posts
Showing posts with label webdev. Show all posts

Monday, July 11, 2016

Devise: redirect to specific pages after sign in using different models

As you should know, Devise can be applied in the same project to different models, but I couldn't find a simple way to redirect users after signing in, based on the kind of resource they were representing (e.g. let's say users and vendors).

After searching for long how to solve this issue and finding long and winding solutions, I found a way to solve it myself in a very simple way; as it is here it's really bare bones and possibly prone to bugs, but it does exactly what I wanted, so I thought other people might find it interesting.

So, let's say we are using Devise on the user and vendor models, and we generated our custom session controllers in user/sessions_controller.rb and vendor/sessions_controller.rb. The first thing that came to my mind was to find a way to customize the redirection from the controllers by overwriting some of the functions, but it turned out to be complicated and not working as expected.

As such I came out with a much simpler even though less elegant solution; that is overwriting the default function after_sign_in_path_for(resource) in the application controller, checking whether in my session I have an instance of one kind of resource or the other.

Here is the relevant code:

class ApplicationController < ActionController::Base
  
  [...]

  def after_sign_in_path_for(resource)
    if current_user
      "your_user_url"
    elsif current_vendor
      "your_vendor_url"
    end
  end
end

I hope this will help you as much as it helped me.

Tuesday, December 15, 2015

The old website challenge (01)

One of the reasons that pushed me to go back updating this blog is a personal challenge I'm taking, which is modernizing an old website of mine, dating back to year 2001! The last big update to the website, namely its port to php from static html, dates back to exactly twelve years ago (December 15th, 2003); I made some other small updates later, up until September 2004.

Later my attention moved to my personal website (www.xfnet.it), leaving the other one untouched until a few weeks ago, when, for several reasons, I got interested again in WebDev, SEO, and the online development world in general.

I will document in this blog the steps I'm taking in order to take the old website into the current times. To be fair, I already took several actions, so the first few posts will be retroactive, but I believe posting every step of the process will help me get a better idea about where I'm going and about the road that I'm taking.

I know, I didn't say which website we are talking about, yet, but let's give it time :)