Rails.application.routes.recognize_path error with undefined method authenticate - ruby-on-rails

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)

Related

'require uri' in not working in Rails 4 app

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"

My Controller can't find my (Action)Mailer!

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.

Rails Routing - redirect_to yielding period rather than backslash

I would like to use the following line:
redirect_to dispensers_url(current_user.id)
to generate the following:
dispensers/13
However I am getting:
dispensers.13
Any idea how to fix this?
The helper should be dispenser_url not dispensers_url. The latter is for the index action.

Get model or 404 on Rails

Is it possible to use a function as get_by_id_or_404 on rails. For exemple, in my controller i use :
#destination = Destination.find_by_id(params[:id])
if the id isn't set or the destination not found, how can I ask Rails to redirect to a 404 page?
Thank you!
In production mode, Rails automatically rescues the ActiveRecord::RecordNotFound exception by rendering the 404 error page.
Simply use the bang version of the finder that raises ActiveRecord::RecordNotFound when no result is found.
#destination = Destination.find_by_id!(params[:id])
However, in your case there's no reason to use the find_by method. Simply fallback to find.
#destination = Destination.find(params[:id])
This is from the guide:
You can specify an exclamation point (!) on the end of the dynamic finders to get them to raise an ActiveRecord::RecordNotFound error if they do not return any records, like Client.find_by_name!("Ryan")
When this happens, Rails will render the 404 page for you. Also, simply use find as a shortcut for find_by_id:
#destination = Destination.find!(params[:id])
I think in production, it will raise the error and display the 404 or 500.

rails: displaying a user friendly message for a routing error in rails

i am working in rails 2.3 on mac osx leopard.
every time i type a url that does not exist in a rails application i get the following error
Routing Error
No route matches "/whatever_i_typed" with {:method=>:get}
this is find for development, but i was wondering how i can make sure users see a friendlier 'oops! not found' page. i was thinking about doing a begin...rescue block but i didn't know where to put it, not did i know the error code (i.e ActiveRecord::RecordNotFound)
thanks!
yuval
This error will never appear in production. Instead, users will see the public/404.html page.
To try this out on your localhost, put passenger/mongrel into production mode. Override the local_request? method on your ApplicationController like so:
class ApplicationController
def local_request?
false
end
end
If you'd like to experiment with dynamic behavior you can check out the rescue_from class method on ActionController.
How about
map.connect '*url', :controller => "not_found"
as a last routes.rb entry? I think it should do the trick, shouldn't it?
I found the below url helpful for Rails 3.0.. users.
http://techoctave.com/c7/posts/36-rails-3-0-rescue-from-routing-error-solution

Resources