-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with helpers #186
Comments
With the script below, everything works fine, except for current_user=(message) method. When calling this method I don't see any output. Neither I get the value back from warden method (though I see the output "warden method called" from this method) I tested this with ruby 1.9.2-p290 and 1.9.3-rc1. Branches master, 0-1-stable and frontier. module MyApp
class API < Grape::API
prefix 'api'
helpers do
def warden
puts "warden method called"
@message
end
def current_user
warden
end
def current_user=(message)
puts "message: #{message}"
@message = message
end
def authenticate!
current_user
end
end
get 'my_email' do
current_message = "something"
authenticate!
end
end
end |
Regarding your first script sample, I ran into a similar issue. It seems that when you add helpers from a module and from a block, you have to add the module first, and then the block. Try this: require 'grape'
module MyHelpers
def say_hello(user)
"hey there #{user}"
end
end
class API < Grape::API
rescue_from :all, :backtrace => true
# first you add your module
helpers MyHelpers
# and then you define your helpers with a block
helpers do
def current_user
"Superman"
end
end
get '/hello' do
# helpers available in your endpoint and filters
say_hello(current_user)
end
end
application = Rack::Builder.new do
map '/' do
run API
end
end
Rack::Handler::Thin.run application, :Port => 3000 I suppose it's a problem in Grape::API#helpers method. I'll try to make a patch, but I can't promise since this is my first Ruby project (still learning metaprogramming). =) |
Can anyone review this patch? I changed the behavior of Grape::API#helpers and added a test for it (which fails without the change). |
…helpers fixes ruby-grape#186: helpers should allow multiple calls with modules and blocks
If someone stumbles on this issue because they are running into helpers overriding each other. You'll be happy to know this pull request fixes that issue. Use the latest master, the current gem doesn't have this fix. Example of overriding bug:
Throws a method doesn't exist error for help1. |
I'm having some problems with helper methods on a server-script. To make it easier, I took this example from grape's documentation.
When I try to call current_user helper-method, I get "undefined local variable or method `current_user' for #Grape::Endpoint:0x1006a0af8"
Anyone else having the same problem?
I get the error with both Ruby 1.8.7 and 1.9.2-p290.
Test script:
Stacktrace:
The text was updated successfully, but these errors were encountered: