Reputation: 6639
I am trying to use a before filter to prevent non-approved users from accessing a page.
To check for non-approved users, I have a method defined in an .rb file, in the /lib folder (I have several methods there that I was in various controllers and views). The method is check_user_for_current_approval(user_id).
In my controller, I tried the following variations (note that I am using RubyMine as an IDE, and it did not give me an indication that I had a syntax error):
before_filter(:only => [:new]) do |c|
c.check_user_for_current_approval current_user.id
end
Which gave me the following error message:
NoMethodError in PaymentsController#new
private method `check_user_for_current_approval' called for #<PaymentsController:0x007ff06784f148>
The following syntax:
before_filter(:only => [:new]) do |c|
c.send (:check_user_for_current_approval, current_user.id)
end
Produces this error:
SyntaxError in PaymentsController#new
/app/controllers/payments_controller.rb:9: syntax error, unexpected ',', expecting ')'
c.send (:check_user_for_current_approval, current_user.id)
^
/app/controllers/payments_controller.rb:9: syntax error, unexpected ')', expecting keyword_end
/app/controllers/payments_controller.rb:130: syntax error, unexpected $end, expecting keyword_end
Using this syntax:
before_filter(:only => [:new]) { check_user_for_current_approval(current_user.id) }
takes me straight to the page (filter has no effect). The different statements came from answers that I've seen on StackOverflow.
Upvotes: 0
Views: 378
Reputation: 4555
Use your first method, but make check_user_for_current_approval
public.
Also:
I think the shared before_filter
would be better suited in the Application Controller, rather than in a lib-file.
That would make it accessible to all controllers that extend the Application Controller, which normally all other controllers do.
Upvotes: 1
Reputation: 1171
Why not use a symbol to define the method to be called?
e.g. PaymentsController:
before_filter :check_user_for_current_approval, :only => :new
def check_user_for_current_approval; raise "Called"; end
Upvotes: 0