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

Комментарии • 56

  • @Deanin
    @Deanin  2 года назад +16

    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!

    • @giordanodiaz5047
      @giordanodiaz5047 Год назад +1

      For the log_out link you can also use this: and works.

  • @yeinercarvajalquina7727
    @yeinercarvajalquina7727 9 месяцев назад

    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!!!

  • @goldenhawk9718
    @goldenhawk9718 2 года назад +1

    Thx i was having troubles with sign out part. Good video

  • @ricardomordaunt1101
    @ricardomordaunt1101 2 года назад +2

    Thank you.. Your tutorials are much appreciated.. Thank you

    • @Deanin
      @Deanin  2 года назад

      These comments are also appreciated 😀

  • @lisapethick2613
    @lisapethick2613 22 дня назад

    so good, so simple - saved me hours - Thank you

  • @TheJonhjonhz
    @TheJonhjonhz 9 месяцев назад

    Very nice, and handy. Enums preferred to be hashes instead of sym arrays. Keep it going man. Thank you for your time

  • @henrikduerrfeld9734
    @henrikduerrfeld9734 2 года назад

    HI, thank you so much this helped a lot. Keep up the good work :)

  • @antoniolu
    @antoniolu 2 года назад +1

    Thanks for improve the RoR community

  • @danielefe3773
    @danielefe3773 8 месяцев назад +1

    truely speaking bro you like Gods sent.. i mean you really are my rails mentor 😎😎😎😎

  • @gamonize543
    @gamonize543 2 года назад +1

    Thank you so much I was looking for this demonstration.

    • @Deanin
      @Deanin  2 года назад

      Glad it helped 🙂

    • @gamonize543
      @gamonize543 2 года назад +1

      ​@@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.

    • @Deanin
      @Deanin  2 года назад +1

      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

    • @gamonize543
      @gamonize543 2 года назад +1

      @@Deanin thank you so much you solved my problem

    • @Deanin
      @Deanin  2 года назад

      Glad it worked! 🙂

  • @josemejias081
    @josemejias081 Год назад +1

    Thaks, great totorial, Good Work

  • @Povai
    @Povai 2 года назад +2

    Great Video

  • @softalxacademy923
    @softalxacademy923 2 года назад

    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?

  • @xaogao
    @xaogao 2 года назад

    thx for the video

  • @brandli3118
    @brandli3118 Год назад +2

    Hey Deanin, how would you change the role of a user in production?

  • @valcaro87
    @valcaro87 Год назад +1

    salamat! 👍

  • @melanievillalobos4007
    @melanievillalobos4007 2 года назад +1

    thousand thanks

    • @Deanin
      @Deanin  2 года назад

      Glad it helped! 🙂

  • @beingmerry
    @beingmerry Год назад

    Thanks for the video! Why was there so many error messages?

  • @Sameer_Kumar
    @Sameer_Kumar 2 года назад +4

    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!!

    • @Deanin
      @Deanin  2 года назад +2

      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.

    • @phyrengr
      @phyrengr 2 года назад

      The dropdown autocomplete you see in the rails console is a Ruby 3 addition to IRB which you get by default.

  • @MohammedAbdulatef
    @MohammedAbdulatef 2 года назад +3

    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.

    • @Deanin
      @Deanin  2 года назад +5

      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. :)

    • @andrehernandez5798
      @andrehernandez5798 2 года назад +1

      The documentation is the best thing you can read in order to learn rails...

  • @adrianfracella1255
    @adrianfracella1255 2 года назад

    Hey Dean, any easy way to hide the button and text or to translate it? I know this is not a rails thing.

  • @mfrederickson1
    @mfrederickson1 2 года назад +3

    Thank you for this. Would you have any interest in taking this one step further and adding confirmation emails for new registrants?

    • @Deanin
      @Deanin  2 года назад +1

      Yup, I can do that. I'll add email confirmations to the list! Appreciate the suggestion :)

  • @BraisonsCrece
    @BraisonsCrece Год назад +1

    Hi Deanin! Can I ask why did you disable turbo links? (noob on board 🙋‍♂️)

  • @AyushDixit-sc5qx
    @AyushDixit-sc5qx 2 года назад +1

    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

    • @Deanin
      @Deanin  2 года назад +1

      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?

    • @AyushDixit-sc5qx
      @AyushDixit-sc5qx 2 года назад +1

      @@Deanin Yeah maybe as of now I'm using rails 7.0.2.3 ,maybe theres some bug ,anyways thank you ,love your video,💚💚💚

  • @nabinakhadka8277
    @nabinakhadka8277 2 года назад

    Thank you foe the video, But must say, Wish you have explained abot in more details, the functionality od the things , and the working

  • @mikelchi8695
    @mikelchi8695 Год назад +2

    what ruby plugins are you using in vscode

  • @pearlseajain3403
    @pearlseajain3403 Год назад +1

    What extension are you using that gives you code suggestion? Love the video!

    • @Deanin
      @Deanin  Год назад

      Combination of Tabnine (free) and GitHub Copilot ($10/mo)

  • @_ShouravChy
    @_ShouravChy 28 дней назад

    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?

  • @stephenw2671
    @stephenw2671 Год назад

    Will this work with Devise in Rails 6? thanks

  • @inhbaonguyen5372
    @inhbaonguyen5372 Год назад

    thank you, but I dont see how and where you define user role,
    example:
    0 = user,
    1 = moderator,
    2 = admin
    Please help, many tks

  • @fugeeohu9357
    @fugeeohu9357 Год назад

    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?

  • @wemersonlucasdasilvapereir8175
    @wemersonlucasdasilvapereir8175 2 года назад +1

    could make a video using devise on rails and views with React on the frontend.

    • @Deanin
      @Deanin  2 года назад +1

      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!

  • @SergioCastellano-lx5bu
    @SergioCastellano-lx5bu Год назад +1

    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

    • @Deanin
      @Deanin  Год назад

      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.

  • @siyaram2855
    @siyaram2855 2 года назад +1

    Why ain't we able to use JIT compiler that comes with ruby 3 for Rails Advantage?

    • @Deanin
      @Deanin  2 года назад

      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.

    • @siyaram2855
      @siyaram2855 2 года назад +1

      @@Deanin what a beautiful language, crippled by speed and concurrency support.

  • @SXCRD
    @SXCRD 2 года назад

    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] )