Create User Account Roles In Devise | Ruby On Rails 7 Tutorial
HTML-код
- Опубликовано: 6 июн 2024
- This tutorial will show you how to add users, moderators, and admins to your Ruby on Rails 7 application. We do this by creating a role integer that we then convert to a readable enum.
This enum may then be used in a similar manner to how we used the admin boolean in my previous Ruby on Rails 6 Devise tutorial. I decided to create this updated Ruby on Rails 7 Devise tutorial to cover a different way to create devise user admins and moderators, as the previous one was less polished.
Join this channel to help support these videos:
/ @deanin
Timestamps
______________________
0:00 Overview Of Roles Added To Devise
0:17 Creating The Devise User Accounts
0:40 Creating The Devise Roles
1:30 Creating The Home Page
2:05 Adding Login, Logout, And Sign Up Buttons For Devise In Rails 7
8:00 Adding Moderators And Admins With Devise In Rails 7
______________________
Follow me on social media:
linktr.ee/deanin
If you liked this video, please consider subscribing: t.co/RZ4EwP0F2a
Timestamps
#Deanin #RubyOnRails #Devise
Decided to update the admin account video with a better system for roles. Some things did change from how you previously used Devise, so I also covered using links to sign up and logout in this scary new Turbo driven world. Hope you enjoy!
For the log_out link you can also use this: and works.
Honestly i'm pretty excited with your channel, I have been working with Rails about 1 year and I consider this videos are awesome. Your work inspires me to be better! Thanks so much Dean!!!
Thx i was having troubles with sign out part. Good video
Thank you.. Your tutorials are much appreciated.. Thank you
These comments are also appreciated 😀
so good, so simple - saved me hours - Thank you
Very nice, and handy. Enums preferred to be hashes instead of sym arrays. Keep it going man. Thank you for your time
HI, thank you so much this helped a lot. Keep up the good work :)
Thanks for improve the RoR community
truely speaking bro you like Gods sent.. i mean you really are my rails mentor 😎😎😎😎
Thank you so much I was looking for this demonstration.
Glad it helped 🙂
@@Deanin How can I set the roles enum value through form. I added
this code in devise registration view which gives no error but when I check the role value. it points to default user.
You'll need to white list the role parameter for Devise specifically.
Leave it as a text_field :role
go to your app/controllers/applicationcontroller
and add this before action and permitted parameter. Notice how the last permitted parameter is your role. This should let you change your role by name I think?
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, :role) }
end
end
@@Deanin thank you so much you solved my problem
Glad it worked! 🙂
Thaks, great totorial, Good Work
Thank you!
Great Video
awesome and precise tutorial, please I notice the devise flash messages does not work as before on signup and logins. do you have a fix for that?
thx for the video
Hey Deanin, how would you change the role of a user in production?
salamat! 👍
thousand thanks
Glad it helped! 🙂
Thanks for the video! Why was there so many error messages?
Good work. Rails 7 and Hotwire are coming with its fair share of nuances.
Btw, what is that utility in your shell for drop-down like autocomplete, really awesome!!
I honestly don't remember what I did, but even while I was recording this I thought "wow, that's really neat," haha.
I'll try to figure out what I did and get back to you.
The dropdown autocomplete you see in the rails console is a Ruby 3 addition to IRB which you get by default.
Absolutely astonishing tutorial.
I just started to learn RoR and I was stuck with the Turbo form. Your video's saved me. On a side note. What are the best sources (books / videos) you would recommend a beginner to take to learn RoR 7.
Thanks a lot.
I appreciate it, and I'm glad it was helpful! There's always a few popular quirks like the Turbo form that seem to trip people up (myself included) with each release.
I haven't looked at any books for Rails recently, so I don't have anything to suggest right now. But let me go ask around and get some feedback, and I'll get back to you. I don't want to go suggesting outdated material. 🙂
As for videos, Chris from @gorails has a ton of usual tutorials and he's actively developing gems that a lot of us use (like noticed for notifications.) He also has some very high quality courses on GoRails that I've heard nothing but good things about.
David Battersby has some great videos covering some popular topics that are often overlooked, like in his Instagram clone tutorial series he covers N+1 query problems. Basically how to stop yourself from needlessly querying your database.
Jason Swett covers a lot of the industry side of testing in Rails, which is also very important to learn as you scale your application.
Brain Trust Digital has some wonderful tutorials that I've used in the last when I was stuck on a topic.
A really important concept I'd get familiar with is using Rails as an API, and learning how to communicate with it using React or Vue. I don't personally use Rails professionally, but it's my understanding that this is how a lot of big applications at companies are run.
Hopefully this helps, I don't have any specific videos off the top of my head, but I'll try to get back to you on books in a few days if I hear back from the people I message. :)
The documentation is the best thing you can read in order to learn rails...
Hey Dean, any easy way to hide the button and text or to translate it? I know this is not a rails thing.
Thank you for this. Would you have any interest in taking this one step further and adding confirmation emails for new registrants?
Yup, I can do that. I'll add email confirmations to the list! Appreciate the suggestion :)
Hi Deanin! Can I ask why did you disable turbo links? (noob on board 🙋♂️)
One of the best Content related Yt Channel ,Great knowledge of rails but the issue of sign_up wasn't resolved by "turbo: false" situation, the same error is showing itself
I just tested this again on my build in a new app and it seems to still work as of Rails 7.0.2.
Maybe there's a difference in our environments somewhere?
@@Deanin Yeah maybe as of now I'm using rails 7.0.2.3 ,maybe theres some bug ,anyways thank you ,love your video,💚💚💚
Thank you foe the video, But must say, Wish you have explained abot in more details, the functionality od the things , and the working
what ruby plugins are you using in vscode
What extension are you using that gives you code suggestion? Love the video!
Combination of Tabnine (free) and GitHub Copilot ($10/mo)
Well..i have followed your video...although it's working fine locally.But when i deployed the project on render it's not showing the role..How to fix this?
Will this work with Devise in Rails 6? thanks
thank you, but I dont see how and where you define user role,
example:
0 = user,
1 = moderator,
2 = admin
Please help, many tks
Obviously this is a way of managing roles without any gems other than devise Has anyone gone this route and found they should start using a gem like rolify?
could make a video using devise on rails and views with React on the frontend.
I'm working on a Devise API + separate React project that let's you log in, but I'll add a embedded react project to the list after I figure out how it all works!
In minute 2:15 you just add the object "current_user", what is this object? where is it declared and implemented? I guess that it's an object which stores the current user but I get errors because I can't reference this object because in my project is not somehow created automatically
Hi! Sorry that I probably didn't explain this in the video. Trying to type out my thoughts on my phone, so sorry if this doesn't make a lot of sense.
Generally in Ruby on Rails, current_user is going to be from Devise. It's effectively the user account of the user who signed in with Devise.
If you don't have a current user, there's a couple potential reasons:
You don't have devise installed.
To install devise and set it up, you should run this in your terminal in your rails app's directory where you run 'rails s':
bundle add devise
rails g devise:install
If you have devise installed, you then want to make sure that you have a devise user created. You can visit app/models/user.rb. If that file exists and has the devise stuff inside of it, you're good.
If current_user still doesn't work at this point, the most likely culprit is that you're not signed in which means it'll return false, or cause an error if you try to access it when not logged in (like if you try to do 'current_user.admin?' when current user is nil because you aren't signed in)
In which case you'll want to visit:
localhost:3000/users/sign_up
Or
localhost:3000/users/sign_in
To create an account and log in. Once logged in, current user should work.
To avoid the nil errors when you aren't logged in, you can wrap things in a
if current_user
else
end
Block of code. You can also do something like 'current_user&.user' to do the nil check in line. It'll just turn false if current user is nil because you haven't logged in.
Why ain't we able to use JIT compiler that comes with ruby 3 for Rails Advantage?
As far as I'm aware, there were plans in Ruby 3.1 to improve the performance over Ruby 3.0 when it comes to Rails and the Just In Time Compiler.
The problem that the JITC was running into, from what I recall, was that Rails would spend a lot of time with it's methods and generate iCache misses.
The app that was originally tested didn't use as many fancy tricks to optimize its workload as Rails did, like I don't think it triggered garbage collection, so the speed hit for rails was a lot higher.
That said, Ruby 3.1 is out now so maybe it's better? But I haven't checked yet, because they pushed the rails release out without realizing Ruby 3.1 was releasing first so Rails doesn't support Ruby 3.1 lol.
@@Deanin what a beautiful language, crippled by speed and concurrency support.
For anyone in the future that finds themselves stuck on the sign out bug, change from link_to to a button_to (and maybe just maybe try to add this to devise.rb: config.navigational_formats = ['*/*', :html, :turbo_stream] )