Admin user: Rails Environment variable

179 views Asked by At

How can one use the rails environment variables within the application?

I am trying to use them to limit access to certain elements on the page. E.g. the users do not see the information an admin does; but the rest of the page is the same. Is there a best practice for such user privilege implementations?

2

There are 2 answers

1
Jakxna360 On

You can use an environmental variable like this:

<%= ENV["Your_Username"] %>

This is an example of what would be in your database.yml. However, like the above user stated, it would be better to put the logic in your view rather than using an environmental variable.

3
lightalloy On

I don't get why you want to use environment variable for it. If you want to limit access to certain elements on the page (or/and actions), you can use gems like cancancan (https://github.com/CanCanCommunity/cancancan) or pundit (https://github.com/elabs/pundit) if auth rules are complex. But if your rules are very simple, you can just do smth like:

if current_user.admin?
... admin stuff...
end

in the view.