Creating and Managing Users and Profile Models in Rails - ruby-on-rails

I know there are multiple threads concerning this topic and having read them I still can't figure out the best approach for my situation. I have a Users model and I am using Devise for authentication. I am using quite a few Devise Modules which have added about 20 columns to my Users model. What I am thinking of doing is to add a separate Profile Model which will have about 18 columns.
The issue is that after sign up, the Devise Confirmable module redirects him to the HOME page and sends a confirmation link to his email. Once he confirms he is taken to the User Show page which is supposed to display his profile information.
I want to ensure that the User fills in his profile information before he is able to do anything on the site. All the Profile fields (About 18 of them) are mandatory and critical from an authorization standpoint and dictate what he can and cannot do. I am not sure how to go about this?
1) Should I just have a User model (this will mean 40 columns in Users table) and create a multi-step form?
2) Should I have a user model and a profile model?
3) If so, how can I make the user fill in his profile information before he confirms his account or right after?
4) Do I need a profile controller as well? I don't know where to begin!

Actually its very simple. You can have different models for user and profile. You need to have some mechanism for flagging if the user has filled his profile information(something like user.profile.nil?). Now you just need to create a before filter method which will redirect the user to create profile page if he does n't have one.

Related

How to make a private chat between 2 users in Rails?

Let's say I have Blog app built with Rails and on a post created by a user(Author) I have a "Request a chat" button.
I want to build a small function on that post page that when User A presses that button, the page will redirect to or open a chatbox that connect User A with user Author?
Author is a devise registered user and User A is not.
How would I build something like that? Thanks
I think it's weird having a devise registered user and a non devise user unless you mean User A is just an unregistered guest. Either way, it's not a big deal and it can be done.
The way you would put together that system is as follows:
OpenChat # your new data model
OpenChatsController # your new controller
"Request a chat" would create a new OpenChat object, with author and guest A foreign keys. If User A is a guest, you can store a cookie "password" in their browser but generally it's only advisable if the conversation is brief and security isn't a big deal.
Then you would be able to check if there is an open chat between the two users and display it in any page you want, and display messages appropriately.
You will need to look up how to setup a basic chat system (there are a million answers out there that will take you step by step) as that's beyond the scope of this question.
If you are new to Rails, I also recommend Michael Hartl's Ruby on Rails tutorial:
https://www.railstutorial.org/

Multiple users authenticating and authorizing

I am a bit confused with Authorizing and Authenticating at the moment.
First I need to tell you about my project. It will have two main models: Users and Pros. Users are people requesting a service. Pros are people offering services. Pros can have very different jobs and thus very different type of data stored in my project. I plan to have different models for different types of jobs held by the Pros (photographers, wedding planners..). Those models will have relationships and "own" different other models (images added with paperclip, links to websites...).
I guess I need to use gems for both Authentication and Authorizing (I have Devise and Pundit in mind)
now my questions:
I would like to have all Users and Pros login through the same form.
I guess this is very a Devise thing. Though after having read a bit about Devise, it seems there is a login for each of the Models. but this thread mentions Devise "groups"
https://gorails.com/forum/devise-with-multiple-user-models-with-single-login-form
Will it definitely solve my problem of a single form login ?
I would like to have each of the Pros submodels show a preview of their records to any type of users or even guests. But when a Pro is logged in they can access to an extended profile view with more information (all personnal data that can be changed, price requests from users, etc...). Can you confirm this is Pundit job here ?
Pundit is the perfect choice for achieving your second point. You can limit certain actions (such as edit/update) to be achievable only by the Pro who owns the account. The code for those actions in the ProPolicy would look something like:
def edit?
update?
end
def update?
record.pro == user
end
In terms of your question about multiple user model authentication using Devise, can you explain why you are set on having just one login form for both users? You could have a dropdown on the login button where the user can select if he is a normal User or a Pro. Or you could even have a checkbox/select on the form where they select which type of login they want to use.
If not, then you will somehow have to check your database to see if the login exists in either the Pro or User tables. However, I suppose that would mean that you cannot have both a Pro and a User account using the same email.

create a profile page for user to view as if logged out

I'm making a Rails application using Devise. On the user profile page, it provides links for the user to update and delete certain elements of their profile. The links are obviously only visible to the signed-in user whose profile it is. The one disadvantage of this is that it doesn't allow the user to view their profile page from the perspective of a visitor, unless they want to log out and navigate to their profile. Some websites offer a "view your profile" link which allows users to view a page from the perspective of someone else. Is there a way to accomplish this with Rails and Devise?
Sure. You are able to create UsersController with show action. Where you will able to display the information you want.
If you want to manage users through CRUD interface there is a wiki page that may help you.
The url to user's profile can looks like /users/:id or you can define custom route such as /user/unique-username (the last example you can achieve using friendly_id gem)

How to create a session#show when using Devise to create a Profile page

first time poster so apologies if I come across very noob like.
I have started developing a TODO Rails app that allows people to login and create several lists for themselves.
I would like to have the user flow work so that after the user signs in they are presented with a "Profile page" that shows some importent links, info on the user and then a grid containing their lists etc.
Now down to the query, What is the best way to go about this as I have a User model and also a list model. Do I need to create a profile controller that has a simple #index action on it that I can pull in info from the 2 models or is there a more acceptable way?
I was hoping that Devise would provide something like this but if they do I cant find it.
I'm not sure that I would couple my user profile page with Devise. Devise is a tool for user authentication, and it seems to me that a user profile page isn't really related to authentication.
I would put your user profile on the user show page, i.e. app/views/user/show.html.erb. (If it doesn't already exist, you might have to create a UserController and app/views/user directory.)
As for redirecting users to the profile page on login, this is (basically) how I do it in app/controllers/application_controller.rb:
class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource_or_scope)
user_show_path(current_user)
end
end
To address your last question, at least partially, no, I wouldn't create a profile controller. Thinking RESTfully, a user profile page would be most appropriate, it seems to me, in UserController#show.

rails 3 - devise block user to login

i have something in mind, i have some user types, Building owner, building manager.
I want to create user as building manager, but i dont want they have access to login system. this user are only for some selectbox in my website, but i need to show them in my user index page.
what i think i can do is create normal user and with a before_save i create a new data in another table.
In a request i need to be able to setup in my building form more than one building manager. maybe the best are with nested form.. I think i will need to add building id to my user table. maybe they can be assigned more than one building.
for now, my db structure are like this :
table users with user data (username, password, email, first and last name, phone)
table usertype have userid, typename and accesslvl
But this problem give me some managing problem. They will not be associated with user data.
How can i resolve this? Does Device can block some user? I searched in the Devise docs, but nothing found.
Thanks for your help.
There is an approach where admin users can approve other user accounts for login. You could use a similar approach but programmatically approve the accounts you actually want to allow logins for. Details are here:
https://github.com/plataformatec/devise/wiki/How-To:-Require-admin-to-activate-account-before-sign_in

Resources