Creating User Profiles - ruby-on-rails

Pretty new to rails, and coding in general. I'm working on my own app right now, in which I'm trying to create user profiles. I know that there have been a few questions about this, but what I'm specifically trying to do is...
Upon authentication (which I've set up using Devise), the user is redirected to his/her profile page where he/she can set up their own profile, with fields similar to what you may find on a social networking website.
I'm running rails 4.1 and ruby 2.1 right now. Does anyone have any suggestions on the best way to go about doing this. As I mentioned I'm fairly new to rails, and have spent the last few days furiously searching for advice on the internet.
Does anyone know of a step by step guide?
I suppose what I need is just guidance on where to start.
Thanks!

You can add other fields like first name, last name, and date of birth to user table itself if you have minimum number of attributes you want to add. You can get all the details from current_user. But if you have many fields that you want to add to user profile then you can create a new model name UserProfile and add other related fields to this model. And add a one-to-one relationship between User and UserProfile table. So in the future if you want to add another relation/attribute with/to user, you can add it with UserProfile. That will reduce making a mess in your database in later stages.

recently i created user profile so that user can add/edit his personal details,see what he has uploaded.what he has liked,posted and edit/delete/update anything that he has created/uploaded.So in user profile all you need is to fetch all details/associated models of that user and show.So as u already have devise..you can get current_user and use user.rb model to understand all associations of user and get the data using associations ans display it on profile page
i have created a profile_controller
so in my nav_bar i have a link_to <% link_to "View Profile",show_profile_path(current_user) %>
where user is directed to profiles controller and i show the details without creating a new model(user has_one :profile)
i have a dedicated page to show profile details + a small page to show any user profile when hovered on users image
take a look here for view side idea... user profile page on bootstrap 3
=======updated part===========
suppose i have a user.rb(using 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
hope this helps

Related

Please explain the has_many, through: source: Rails Association

I've found a bunch of articles, stackoverflow answers and rails documentation about 'source:', but none of it explains this association in a way I can understand it. I need the most simplified explanation of this way of associating, if possible.
My example is this:
Album:
has_many :reviews, :dependent => :destroy
has_many :reviewers, through: :reviews, source: :user
belongs_to :user
Review:
belongs_to :album, optional: true
belongs_to :user
User:
has_many :reviews
has_many :reviewed_albums, through: :reviews, source: :album
has_many :albums
The rest of the code does not mention "reviewers" or "reviewed_albums", so that is the part I understand the least.
Are those names completely irrelevant?
TL;DR
source is standing for the table that this association is referring to, since we give it a different name than just .users because we already have the belongs_to :user association.
Long explanation
I think it's easiest with this little picture which is basically the database schema for the models you posted above.
We have albums, that belong to users, meaning that a user is basically someone who creates an album. We also have reviews and they belong to albums, meaning an album can be reviewed. And a review is made by a user so that's why a review belongs to a user.
Now associations in rails is a way to create methods that can be called on a database record to find its associated record.
We could get a user from an album or all the reviews a user made for example.
album = Album.find(1)
album.user # => returns the creator of the album
user = User.first
user.reviews # => returns all the reviews a user made
Now there is even more connections between those models than the ones mentioned above.
Let's look at album first:
# album.rb
has_many :reviews, :dependent => :destroy
belongs_to :user
has_many :reviewers, through: :reviews, source: :user
An album belongs to one user who created it. It has many reviews. And, if we we follow the line from albums to reviews and then further along to the users table, we see that we can also access the users that gave the reviews.
So we would want to do something like
album.reviews.users
Meaning: give me all the users that left a review for this album. Now this line of code wouldn't work - because album.reviews returns an array (an ActiveRecord::Relation object to be exact) and we cannot just call .users on this.
But we can have another association
has_many :reviewers, through: :reviews, source: :user
And here we're calling it reviewers to not get confused with the method/association .user that refers to the creator. Normally, Rails would refer the database table name from the name of the association. Since we're giving a different name here, we have to explicitly give the name of the DB table we're referring to and that is the users table.
So this is what this line is about - we create another association, we don't want the direct line (see image) between album and user, we want the users that left a review on this album, so we go through the reviews table and then we have to give the name (source) of the table so Rails knows in which table to look.
And this will finally allow us to write code like this:
album = Album.first
album.user # => creator of the album
album.reviewers # => all users that have left a review for this album
Hope that helps! Let me know if you have any more questions.
Maybe you can explain the other association with source in the users model in the comments.

Beta Invite Codes Rails

I've seen some great resources for creating invitation systems where the app sends an email invitation with a link to the invited user's email, like devise_invitable.
What I'm trying to figure out is how to generate a bunch of invite codes so that I can give them out to specific people, and then they can sign up with that invite code. I have some ideas of what I would do but I'm wondering if anyone's every come across this before and has some tried and true methods.
I'm using devise for authentication by the way.
Any help is much appreciated.
You could always create an InviteCode model that contains a randomly generated redeemable code that can be issued on demand and validated at a later point in time.
For example:
class User < ActiveRecord::Base
has_one :invite_code_used,
:class_name => 'InviteCode',
:foreign_key => 'user_redeemer_id'
has_many :invite_codes,
:foreign_key => 'user_creator_id'
end
class InviteCode < ActiveRecord::Base
belongs_to :user_creator,
:class_name => 'User',
:foreign_key => 'user_creator_id'
belongs_to :user_redeemer,
:class_name => 'User',
:foreign_key => 'user_redeemer_id'
end
You'd create a randomly generated string to use as the invite code, presumably somewhere like before_validation to ensure it's populated before saving. When the code is redeemed, link the code to the user that was created so you can see who actually claimed it.
Creating an invite code for a user is as simple as something like this:
#invite_code = #user.invite_codes.create(:email => 'someone#example.com')
You can add some validations on the creation of an InviteCode to ensure that a given user hasn't created more than they should've and any other business logic you might require.

Delete Rails model and its associations in one action? NEWBIE question

I have a really simple association with the Devise user object where each user has one Profile (with more application specific stuff...) I am having no issues creating the User object and accessing the user and its profile object. i.e.,
#user.profile
However, I'm having an issues when I try to delete the profile object - I'd assume that when I delete the User object, it would also delete each associated object. The association in my User object is like so
accepts_nested_attributes_for :profile, :allow_destroy => true
The has_one and belongs_to associations are set on both the User and Profile objects. Maybe the issues is in Devise code - I'm stumped. An idea what I'm missing here.
You need to specify :dependent on the association:
has_one :profile, :dependent => :destroy
Look Association for more information.

Rails 3 set parameter of belongs_to association

I have a rails app that is tracking social data. The users are going to be able to create groups and add pages(ie. facebook fan pages) to their groups by the page's social id. Since users could potentially be adding the page as someone else, I have it set up so that there is only one page per social id. I also have a pivot table called Catgorizations that links of the pages to the groups and users.
My model relationships are set up as follows:
User
has_many :groups
Group
belongs_to :user
has_many :categorizations
has_many :pages, :through => :categorizations
Page
has_many :categorizations
has_many :groups, :through => :categorizations
Categorization
belongs_to :group
belongs_to :page
Now when I create a new page and it saves, it is creating a new categorization. The problem I'm running into is that in the Categorization I need to set the user_id manually. I've tried:
#page.categorizations.user_id
But I get an undefined method user_id error. I may be approaching this from the wrong direction but how would I go about setting the user_id of a categorization through the page object?
Also if it matters, I'm using devise to handle my user management stuff.
Any help would be greatly appreciated.
Thanks
What you're tring to do is to access something several levels deep in a 'chained' set of relationships.
In order to access an instance of the User model, given a page, you need to get its categorizations, pick one (how?), get its group, then see what the user_id is of that group.
Conceptually a page will actually have many users that might be in charge of categorizing it.
To get something to happen you could arbitrarily pick the first categorization and then do something with its user:
cat = #page.categorizations.first
user = cat.group.user
I don't know what you mean about 'setting' the user id for the categorization - it doesn't have a user, so I don't know what you'll then want to do with that information, sorry!
From your description and your model, only Pages have a User, not Categorization, which is why you get the error that it doesn't exist (it's not in your database).
Also, you are missing the opposite association on Page:
Page
belongs_to :user
This allows you to get back to User from Page:
#page.user.id

Rails Photos, Users, Comments

It's driving me crazy. I have 3 models. User, Photo, Comments.
Here is what I want to do.
A user has many photos and comments
A photo belongs to a user and has many comments
And a comment belongs to user and to a photo
So my models have these associations:
User
has_many :photos, :dependent => :destroy
has_many :comments, :dependent => :destroy
Photo
belongs_to :user
has_many :users, :through => :comments
has_many :comments, :dependent => :destroy
Comment
belongs_to :photo, :user
I want now to show a photo and load all the comments of this photo and display each comment with the user info.
So at the photos controller show action I have
#photo = Photo.find(params[:id], :include => :comments, :order => 'comments.created_at DESC')
And in the photo/show view
=render :partial => "/comments/partials/comment", :collection => #photo.comments, :as => :comment
It display the comments eg. the comment text fine but when inside the partial I try to do:
%p=comment.user.fname
%p=comment.body
It throws error "undefined method `fname' for nil:NilClass"
Something strange is that I am user authlogic so you have to be logged in to post a comment. But you can see the comments even if you are not logged in. When I am logged off it works find. When I log in it throws error.
Any help would be much appreciated cause it is driving me nuts.
By the way in my routes I have
map.resources :users, :has_many => [:photos, :comments]
map.resources :photos, :has_many => [:comments, :users]
Thanks
Not sure this will make a difference, but have you tried separating the belongs_to associations?
belongs_to :photo
belongs_to :user
+1 for using haml
i can think of a few reasons why this might happen
firstly, what happens when your remove the following line from your code?
# try removing this
%p=comment.user.fname
Does the error then move on to next variable (i.e. comment.body)
If not, then you have at least narrowed it down to the fname variable. in which case I would wonder if you have perhaps added the fname variable to your model after creating some intitial database entries... this would mean that some of hte entries don't have associated fname variables. in this instance you can fix the issue by scrubbing the database and remigrating from scratch
Also, do you have attr_accessible set for the fname variable in your model? chekc that you have this set for all the variables.
can you look inside your db and make sure that all entries have an fname variable set?
I realise that you want to get this working but if you can't, there's no shame in using the disqus.com plugin - it saves you db space, helps to attract more comments as lots of people already have profiles and gives you some nifty moderator features... on the downside, you lose branding, and you can't use any of your own rjs effects..
good luck

Resources