Sunday, December 12, 2010

Devise conflicts with session.rb model

Sessions vs "sessions"

Wikidiscography.com is a session-based discography. As such, it has a model called session.rb along with the corresponding controllers and views. These sessions are not internet/computer/user/browsing sessions, but recording sessions. So I already have a model/controller/view set called "sessions."

When I installed the Devise authentication system for the first time, I got an error when I tried to sign in after creating a new user.

Sign in error

To sign in as a user, one navigates to /users/sign_in. This is a route supplied by Devise to sign in using the credentials already existing in the database. When we click okay, the following error appears: 'Routing Error No route matches "/sessions/user"'.

After signing in with valid credentials

Notice the URL that it redirected to after the attempted sign-in: /session/user. I already have a session model and a sessions_controller, so as I have the application currently set up, this will cause Rails to look for a "user" action in the file sessions_controller.rb. Devise is hitting a resource conflict.

Solution

One way to figure this out is to rename MY session resources to "music_session." I go over how to do this in a separate post. With "session" renamed to "music_session" and "session_type" renamed to "music_session_type" everywhere in the application, Devise is now able to sign in an existing user successfully.

This is not necessarily the best or only way to solve this problem. A more advanced Rails user (not me) might be able reconfigure the routes that Devise uses so that it avoids the "session" part of the url. But by doing it this way I was able to let Devise have its default settings. Now everything "just works." Thanks Rails. Thanks Plataformatec (makers of Devise).

No comments:

Post a Comment