开发者

RoR: Devise and Profiles

开发者 https://www.devze.com 2023-03-12 08:34 出处:网络
I\'m a complete Rails noob and I\'m working on my first rails app. I\'d like each user to have a profile, separate from the users table, but linked by id. I 开发者_开发技巧did this alright in PHP bef

I'm a complete Rails noob and I'm working on my first rails app.

I'd like each user to have a profile, separate from the users table, but linked by id. I 开发者_开发技巧did this alright in PHP before, but rails syntax is new. :/

How do I create a profile for each user when they register via devise? And how do I link their users/edit page to edit their profile instead?


You should read this tutorial about Relationship

It's really easy to declare associations in Rails. In your app/models/user.rb, you could do something like that:

has_one :user_profile

Your user's profile is a different object with its own table. Just make sure that you had the foreign key user_id in it, and you're good to go (also, you should specify belongs_to :user in your user's profile model).

Now, using Devise, if you want to make sure that a profile is being created after a user registers, you could do something like that (still in your user's model):

after_create :create_child
# Creating child Elements
def create_child
  UserProfile.create("user_id" => id)
end

And then, if you want to 'link' a specific URL to a controller, see the routing tutorial

Hope it helps.

0

精彩评论

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