开发者

rails 3 + devise, probably pretty easy: how link a (app-specific) Customer object to a (devise-created) User

Our app (before authentication) has Customers and Widgets. As you'd expect each customer has a single record in the Customers table, and we don't want customers to get into each other's accounts.

I was able to开发者_如何学运维 get devise for rails installed and working fine. (The most helpful tutorials IMO were http://presentations.royvandewater.com/authentication-with-devise.html and http://asciicasts.com/episodes/209-introducing-devise )

our root page on our site (Home#index) simply displays the register or sign-in links.

if user signs in we want to take them to the CustomerSController show method for THEIR account...

so I need to do three things, none of which I see how to do with Devise:

a) when a User registers, and User record is created, also create a new blank Customer record (I know how to create a Customer.new, I just don't see where to 'put' that code)

b) associate that brand new Customer record with their User record (1 to 1) -- should I put the customer.id in users or the other way around?

c) when they sign in, direct them to

customers/[theUser.customer.id]

(or something like that)


a & b) You have to use after create hook (something like this):

class User < ActiveRecord::Base
  has_one :customer
  after_create :assign_customer

protected
  def assign_customer
    customer.create
  end
end

c) And then in registrations controller add this:

 def after_inactive_sign_up_path_for(resource)
   customer_path(resource.customer)
 end


You can overwrite devise controller, like class RegistrationsController < Devise::RegistrationsController and put there everything you want, including redirect and others

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜