创建用户配置文件

时间:2014-08-19 04:57:21

标签: ruby-on-rails

相当新的rails和编码。我正在开发自己的应用程序,我正在尝试创建用户配置文件。我知道有一些关于此的问题,但我特别想做的是......

验证后(我使用Devise设置),用户被重定向到他/她的个人资料页面,他/她可以在其中设置自己的个人资料,其字段类似于您在社交网站上找到的字段

我现在正在运行rails 4.1和ruby 2.1。有没有人对最佳方法有任何建议。正如我所提到的,我对铁路相当陌生,过去几天一直在疯狂寻找互联网上的建议。

有没有人知道一步一步的指南?

我想我需要的只是指导从哪里开始。

谢谢!

2 个答案:

答案 0 :(得分:1)

如果您要添加的属性数量最少,则可以将其他字段(如名字,姓氏和出生日期)添加到用户表本身。您可以从current_user获取所有详细信息。但是,如果要将多个字段添加到用户配置文件,则可以创建新的模型名称UserProfile,并将其他相关字段添加到此模型中。并在one-to-oneUser表之间添加UserProfile关系。因此,将来如果要向/用户添加其他关系/属性,可以使用UserProfile添加。这将减少在后期阶段使数据库混乱。

答案 1 :(得分:0)

最近我创建了用户个人资料,以便用户可以添加/编辑他的个人详细信息,查看他上传了什么。他喜欢,发布和编辑/删除/更新他创建/上传的任何内容。所以在用户个人资料中你需要的是获取该用户的所有细节/相关模型并显示。因为你已经有了设计..你可以获得current_user并使用user.rb模型来理解用户的所有关联并使用关联来获取数据在个人资料页面上显示

我创建了一个profile_controller 所以在我的nav_bar中我有一个link_to <% link_to "View Profile",show_profile_path(current_user) %>

其中用户被定向到个人资料控制器,我显示详细信息没有创建新模型(user has_one :profile

我有一个专门的页面来显示个人资料详情+一个小页面,以便在用户图像上悬停时显示任何用户个人资料

在这里查看视角方面的想法...... user profile page on bootstrap 3

=======更新部分===========

假设我有user.rb(使用devise

###different associations of user can be:=
 ##this user can like/dislike any model having acts_as_votable
acts_as_voter
##user can tags post/audio/videos/images
acts_as_tagger
##my user can create post,pages,upload images/videos/songs/locations etc...
has_many :posts, :class_name => 'Post', :dependent => :destroy
has_many :pages, :class_name => 'Page', :dependent => :destroy
has_many :images, :class_name => 'Image', :dependent => :destroy
has_many :videos, :class_name => 'Video', :dependent => :destroy
has_many :audios, :class_name => 'Audio', :dependent => :destroy
has_many :places, :class_name => 'Place', :dependent => :destroy
has_many :profile_pictures, :class_name => 'ProfilePicture', :dependent => :destroy


#####so..as now i know that **my user can have post/pages/audios/videos** etc...i can also get the details about those associations(assuming you have configured belongs_to in associated models as well + tables with foreign_key as user_id in eash associtated table),such as:-*

##to get all  videos that user has uploaded
@user.videos
##same applies for other associated models as well
@user.images..@user.audios...@user.pages.....@user.post....@user.places...@user.profile_pictures...etc
####So now you have all user(current_user data)...
###its time to show data in profiles_controller..
##your show method
## profile#show
def show
@user=User.find current_user.id
@all_user_videos=@user.videos..and so on
##get all user videos/images..etc and show it in your view file
##your view file can be the one that i shared the link or any other way that you want...
###there are lot of ways...google around and then you can get an idea as how to display user_profile page..as now you have the data(*Yipee*)

end##method ends

希望这会有所帮助