Admin- Yelp

So I got through the Yelp, and we set up all the permissions. How do I set who is an admin to add a new restaurant? Am I missing something easy here? Thanks,

Posted over 4 years ago by Scott Wilson
Posted over 4 years ago by Alex Yang

Hey Scott,

The Rails console is perfect for this. If you haven't had much experience with it, you can think of it as one of the two ways to make changes to your database (which is really what you're doing when you create new Users, add new Restaurants, etc). You can either do these through your app itself (assuming you've built this functionality into your site), or you can do this through the Rails console where you have a lot more flexibility to write code and make direct changes to your database.

So to set an admin user on localhost, you'd access the Rails console by running the 'rails console' command from your Command Line (or 'heroku run rails console' on your production app). You'd then find the user you like with a command like '@user = User.find_by(id: 5)'. Alternatively, you could find by another field, like '@user = User.find_by(email: "alex@baserails.com")'. Once you've found the user you want, you can give them admin privileges (assuming you already set up an 'admin' field in your User database) with '@user.admin = true'. Finally, run '@user.save' to save this change to the database. You're all set - just hit Ctrl-D to quit out of the Rails console.

Let me know if that makes sense!

1
Posted over 4 years ago by Scott Wilson

Great! How would I set this up for the heroku version? Is this covered in one of the lessons and I just forgot about it?

Also can you point me to some documentation that would help me set this kind of functionality up on the site and not in the console?

Thanks again,

1
Posted over 4 years ago by Alex Yang

I'm afraid there isn't any documentation to build out this feature because there's no way to avoid using the Rails console in the process. What I mean is you probably don't want any ordinary user to be able to give themselves admin privileges, so you could create a way for admin users on your site to designate other users as admins as well. But then how do you create your first admin user? You'd have to use the Rails console.

1
Posted over 4 years ago by Scott Wilson

Haha just read over what you wrote, never mind on the heroku question!

1