I am trying to call a method inside a namespaced module. This is what I am trying to do, however I get "undefined method" error. Calling the module method without the send method is working correctly.
variable = send "Namespace::#{type.capitalize}Helper.#{type}_method".to_sym, params
Thanks for any help.
(Rails 4.1, ruby 2.1.1)
variable = "Namespace::#{type.capitalize}Helper".constantize.
send( "#{type}_method".to_sym, params )
(broken into two lines, should still work because of the trailing ..)
send takes a method name, not the whole Classname.method code. You're now doing:
Namespace::SomeHelper.send( "sometype_method".to_sym, params)
Related
I am new to ruby on rails.
I am trying to get haml and wice_grid to work together. I am using this example as a model:
http://wicegrid.herokuapp.com/basics3
I get the error 'undefined local variable or method `show_code' for...'
In the file app/views/basics3/index.html.haml which you can see at the link above.
Am I missing a gem? In general, what is the best way to troubleshoot problems like this?
Thanks in advance-
Flex
EDIT: I found the definition for show_code. It's in a helper that I found in the unit tests for wice_grid.
https://github.com/leikind/wice_grid_testbed/blob/master/app/helpers/application_helper.rb
That said, I get more errors when I load it into my project. So the question becomes, how does the helper normally get included in my project?
show_code is a custom method created just for the example page you linked to. It just displays the code he has in his controller and his index and grid views. You don't need to call that method in your own application so just remove that line and you should be good.
Trying to "require 'uri'" in a utility class and it doesn't seem to be loading. Keep getting the error:
NoMethodError at / undefined method `query' for "https://api.twitter.com/1.1/search/tweets.json?q=%40twitterapi":String
Whereas the .query method should become available via: http://www.ruby-doc.org/stdlib-2.0/libdoc/uri/rdoc/URI.html
Any ideas on what I'm doing wrong?
The method you are trying to call is only available on the URI object, but you are trying to call it on a string. (the string representation of the uri)
If you want to be able to use those methods, you need to create a URI object.
uri = URI("https://api.twitter.com/1.1/search/tweets.json?q=%40twitterapi")
uri.query # => "q=%40twitterapi"
I am using rails 2.3.8 . I need to call a controllers action from a initializer.
I found this snippet in rails 3 usin Proc
OmniAuth.config.on_failure = Proc.new do |env|
UserSessionsController.action(:omniauth_failure).call(env)
end
If i use this in Rails 2.3.8, it returns this error
undefined method `action' for UserSessionsController:Class.
It seems Rails 2.3.8 does not have "action" method.
Is there any other way to call an action from initializer in Rails 2.3.x?
I'm not sure why you would want to do this, but controllers are just classes. So something like
TestController.new.index
will call the index method.
Keep in mind that the controller will be initialized w/o all the info that normally comes in w/ a request, so its very possible that things will break.
Greeting all...
I'm trying to figure out a problem that I've never seen and shouldn't be happening by all accounts...
I'm using Rails 3.0.12 with a simple/standard ActionMailer setup:
I have HelpRequestMailer in app/mailers
I have HelpRequestsController in app/controllers
First problem... When I call the mailer in my controller...
help_request = HelpRequest.new(params[:help_request])
...
HelpRequestMailer.help_request_email(help_request).deliver
I get the following error: NameError (uninitialized constant HelpRequestsController::HelpRequestMailer)
This in itself is strange.
When I add the following...
require File.expand_path('../../mailers/help_request_mailer', FILE)
...To the top of the controller (I would expect the path to be '../mailers/help_request_mailer' but that doesn't work) - which I shouldn't have to do - the controller seems to find the mailer but doesn't seem to recognize what it is/know what to do with it. I get the following error:
NoMethodError (undefined method `help_request_email' for HelpRequestMailer:Class)
Which is technically true... There's no help_request_email class method in HelpRequestMailer... It's an instance method (as specified by the documentation).
My ActionMailer configuration lives in config/application.rb
Any help/suggestions would be greatly appreciated...
I had this problem too, and it turned out to be a case of a completely unhelpful error message. In my case it was just a syntax error in some of my code for the action mailer or the associated view.
I think what happened is that when Rails couldn't properly parse the mailer or view code, it just bypassed the files and never instantiated the action mailer object, leading to the error in the controller.
I would have much preferred it if Rails had tripped on the error in the action mailer code itself.
I am using Rails 2.3.2 and I am really miss something important in here.
I got some .rb files in my lib folder and in one of them, I got a line saying
User.new(x,y,z)
But when I go to the user.rb which is also sitting in lib folder, I don't find any method with defined with new. When I look at the initializer, it just assigns the incoming attributes to the variables like
def initialize(x,y,z)
#x = x
#y = y
#z = z
end
Can you guys please tell me what I am really missing here. I know I am missing something really important.
Thanks
In Ruby (not only Rails) calling ClassName.new() invokes initialize method from this class to ... well.. initialize the object created. The initialize method will be passed all the arguments that are passed to new()
See here for details: http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html