El miércoles, 8 de marzo de 2017 10:16:30 (CET) Chee Chong Hwa escribió:
> Hi Sergio
> 
> 1. In my seesions controller
> def create
>     user = User.find_by(username: params[:username])
>     if user && user.authenticate(params[:password])
> *$level*=user.level
>     end
> end
> 
> 2. in my tasks controller, I am trying not to show the Crete New if
> user.level>0
> 
> active_scaffold :"task" do |conf|
>   if $level > 0
>   conf.create.link = false
>        end
> end
> 
> Result, the $level >0 is not honoured.

Two things:

1. You shouldn't use global variables, if really needed, better use 
Thread.current hash, but 
you should know it may be read for next requests, if same thread/process is 
used. To clean 
thread variable, use request_store gem.

2. active_scaffold block is evaluated when class is loaded, at app start in 
production, 
before actions are processed. So you cannot set anything in action and use it 
in 
active_scaffold block. You have 2 options for removing create link for each 
request:

- Add :ignore_method option to action link (conf.create.link.ignore_method = 
:controller_method). If that method returns true, link won't be displayed. 

- Add before_filter to remove link (active_scaffold_config.create.link = 
false). It's harder 
because it may work different in production, because controller is not reloaded 
on every 
request, so after removing create link, next requests won't have create link, 
so you have to 
add if then else to add or remove link. Per request configuration is explained 
in wiki 
https://github.com/activescaffold/active_scaffold/wiki/Per-Request-Configuration[1]
 but it 
should be avoided if possible, it's easier to use ignore_method for your use 
case.

> 
> Q. How can I get this to work. I am using levels for role-based security.
> Ever user has a level=0-100
> 
> TIA

In that case you could use current_user.level (or whatever method to get 
current_user 
defines your authentication code) instead of using global variable.


--------
[1] 
https://github.com/activescaffold/active_scaffold/wiki/Per-Request-Configuration

-- 
You received this message because you are subscribed to the Google Groups 
"ActiveScaffold : Ruby on Rails Gem" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/activescaffold.
For more options, visit https://groups.google.com/d/optout.

Reply via email to