Reputation: 2172
I'm building my first gem
. I'm trying to access all the parameters when a view loads, and then assign them to my module methods.
What should happen is when I visit appurl.com?name=elijah
and then run MyGem.name
it should return elijah
Here's my gem
#my_gem/lib/my_gem.rb
module MyGem
def self.name
params[:name]
end
end
How do I access the params of wherever I'm calling my gem from? Do I have to pass it in as a variable?
Upvotes: 0
Views: 247
Reputation: 23959
Yes, you have to pass them into something in your gem. Your gem can't see the params
method in ActionController
.
What are you trying to accomplish? We may be able to suggest a better approach.
If you're just experimenting, try something like this:
module MyGem
class Something
def initialize(params)
@params = params
end
def method_missing(symbol, *ids)
@params[symbol] || super
end
end
end
This will pull items from the params hash given to it, if they are available as a symbol in the hash. So you could do something like:
x = MyGem::Something.new(params)
x.first_name
=> 'xyz'
x.last_name
=> 'abc'
Upvotes: 1