'require uri' in not working in Rails 4 app - ruby-on-rails

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"

Related

ruby on rails constantize method

I came across the following in the model:
class Search < ActiveRecord::Base
#search different system user by dn
def self.gets(sys, dn)
sys.constantize.search(dn)
end
end
I can see the purpose is to pass in different model name as sys and search by dn in those specific models. However, I searched on constantize in Ruby and couldn't see any detailed explanation about this usage.
Rails documentation (because constantize is a Rails method) says:
Tries to find a constant with the name specified in the argument
string.
For instance, if you have a model called Foo in your application, then you can apply the constantize method to a string, which contains the exact word Foo, and it'll give you a new object with this model. Note this must be capitalized as Rails would work with your model, if you do a bad reference, then you'll get a NameError error:
NameError: wrong constant name foo
How does it do it?, if you go to the method definition, or if you playing with the method get an error, you'll see the source points to activesupport-5.1.5/lib/active_support/inflector/methods.rb:269:in 'const_get', which is the method definition and the error source.
Between the cases the method handles internally depending on what's being received as argument, you'll see the Object.const_get(string) which is the way Ruby (pure) handles a "constantiz-ation", which would be the same as doing
Object.const_get('Foo') # Foo(...)
Object.const_get('foo') # NameError: wrong constant name foo
If thinking on implement this handy method, you could take a look to the Gavin Miller's post from some years ago.
When I type 'foo'.constantize in my editor (RubyMine) and tap Ctrl+B, it takes me to the source, .../activesupport-4.2.8/lib/active_support/core_ext/string/inflections.rb, with this comment:
# +constantize+ tries to find a declared constant with the name specified
# in the string. It raises a NameError when the name is not in CamelCase
# or is not initialized. See ActiveSupport::Inflector.constantize
#
# 'Module'.constantize # => Module
# 'Class'.constantize # => Class
# 'blargle'.constantize # => NameError: wrong constant name blargle
def constantize
ActiveSupport::Inflector.constantize(self)
end
It probably eventually calls Object.const_get(). I recommend you get a better editor, with code browsing, and you start learning your way around the Rails source code.

Rails send method to namespaced module

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)

Rails.application.routes.recognize_path error with undefined method authenticate

In my application_controller.rb I have a line
# Get the previous url string nicely.
previousPath = Rails.application.routes.recognize_path(request.referrer)
But I get this error on that line
NoMethodError (undefined method `authenticate?' for nil:NilClass)
Which seems like something to do with devise.
But when I do include Devise::TestHelpers, I get env is not defined or something, which doesn't seem like a good solution in any way possible.
Any ideas on how to solve this without needing me to catch errors or regexp to split the path?
This issue may be due to route path which is written in routes by default as and when the application loads first, so join the string to the url you get from request.referrer.
ie, url = request.referrer + "controller_name"
previousPath = Rails.application.routes.recognize_path(url)

Why route method not found 'change_password_admin_user' using ActiveAdmin / Devise?

Can't understand how this method isn't found?
undefined method `change_password_admin_user' for #<ActiveAdmin::Views::ActionItems:0x007ff1cad9d848>
Output of my rake routes
change_password_admin_user GET /admin/users/:id/change_password(.:format) /users#change_password
Appears that you don't have used the suffix in the method call. Two methods are generated by the route that you pasted in in your question:
change_password_admin_user_path
Which return a path relative to the domain, and the other version which includes the host of your site:
change_password_admin_user_url

Calling ERB without Rails: undefined method 'raw'

I am using the ERB engine to generate an offline HTML version of a page of my Rails website. The page shows great when shown by Rails, but I have trouble generating with ERB by myself (despite using the same ERB template).
First I was getting the error undefined method 't' and I solved it by replacing all <%=t(...)%> calls with <%=I18n.translate(...)%>.
Now I get undefined method 'raw'. Should I replace all <%=raw(...)%> calls with something else? If yes, what?
raw is defined as helper in actionpack/action_view library so that without rails you can't use it. But ERB templating shows its output without any escaping:
require 'erb'
#person_name = "<script>name</script>"
ERB.new("<%= #person_name %>").result # => "<script>name</script>"
And because of this for purpose of escaping there is ERB::Util#html_escape method
include ERB::Util
ERB.new("<%= h #person_name %>").result # => "<script>name</script>"
While #warhog 's answer will work, the include isn't necessary. It adds all the ERB::Util methods to the current class, which usually isn't desired and can cause unexpected side effects (if you had another h method for example). Instead just access the h method (or other helpers) using the ERB::Util class:
ERB.new("<%= ERB::Util.h #person_name %>").result

Resources