Rails route issue after upgrading to rails 6.1 - ruby-on-rails

After upgrading to Rails 6.1, I'm getting following error:
undefined method `find_script_name' for nil:NilClass
In this case the route is being used is root_path but getting this for many other routes too! routes.rb is as following (tried like this after removing all other route definitions)
Rails.application.routes.draw do
root 'home#index'
end
Only relevant thing I found online is this commit. Anyone has idea what could be wrong?
I'm running on ruby 2.7.2

it looks like a bug in the current release of rails 6.1
https://github.com/rails/rails/issues/42218
the current fix is to prefix all path helper calls inside the views with:
Rails.application.routes.url_helpers.
so session_path becomes Rails.application.routes.url_helpers.session_path

Related

Included route helper not working after upgrade to Rails 6.1.6

The following code used to work in Rails 6.0.4.8 but no longer works in 6.1.X (I tried all minor versions, even the latest 6.1.6 has this behaviour; the engine itself is legacy code, unfortunately, we can't remove it easily):
# config/routes.rb
Spree::Core::Engine.routes.named_routes.url_helpers_module.include(DynUrlHelper)
MyApp::Application.routes.draw do
mount Spree::Core::Engine, at: "/"
end
# app/helpers/dyn_url_helper.rb
module DynUrlHelper
def dyn_path(target)
# ...
end
end
It throws an undefined method error when I try to use #dyn_path in a controller or view:
undefined method `dyn_path' for #<MyController:0x00007facc9227298>
Upon investigating the issue, I noticed, that when I change a file and Zeitwerk reloads my code, it works! But a fresh started app fails. So I guess it has something to do with caching the named routes helpers?
The only thing I found is this question Rails route issue after upgrading to rails 6.1 linking to this Github issue https://github.com/rails/rails/issues/42218. But I'm not sure if it really is the same problem.

Rails route sometime got an error not found

My Rails route sometimes get a not found error and resolves itself after a few seconds.
undefined local variable or method owner_root_path' for #<Owner::SessionsController:0x00007f30408d46f0>
/myapp/app/controllers/owner/sessions_controller.rb:30:increate’
/ruby/2.5.0/gems/actionpack-5.1.6.2/lib/action_controller/metal/basic_implicit_render.rb:4:in send_action'
/ruby/2.5.0/gems/actionpack-5.1.6.2/lib/abstract_controller/base.rb:186:inprocess_action’
My routes config
# routes/owner.rb
Rails.application.routes.draw do
constraints subdomain: /^owner/ do
root to: "owner/top_pages#show", as: :owner_root
...
end
end
# application.rb
config.paths["config/routes.rb"] = %w(
config/routes/owner.rb
config/routes.rb
).map {|relative_path| Rails.root.join(relative_path)}
Does anyone know why it happened?
Rails 6.1 guides introduced the draw macro which can be used to split a large route file into smaller files. You can read about it here: Breaking up very large route file into multiple small ones
I don't know whether your version of Rails supports this macro. If it doesn't, then you can easily define a draw method yourself, using the source. It will be something like this:
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
end
P.S: You should definitely check out the commit that introduced this method/macro.

rails route not working but not sure why

Another adopted rails app issue. I have the following in my routes.rb file. It is currently not working (not sure if it ever was). We have upgraded from 3.1 -> 3.2.13
scope '/arc' do
match '/api/get-images-by-location/:global_id' => 'api#get_images_by_location'
end
and I get the following error:
ActionController::RoutingError (No route matches [GET] "/arc/api/get-images-by-location/168"):
Should this route be working? I think it should but clearly it isn't.
thx for any help
edit 1
Added namespace for arc

Rails newbie: How to add routes to a rails 3 engine?

I'm trying to write my first rails 3 gem - everything works well, except for routes - I can't seem to get them working. It's possible this is a very simple error - as mentioned, it's my first experience with engines. The gem itself is very, very basic - literally just one scaffold
My gem's config/routes file:
class ActionController::Routing::RouteSet
resources :frogs
end
...And when I try to start the server, I get the following error:
/home/john/.rvm/gems/ruby-1.9.2-p0/gems/cancandevise-0.1.0/config/routes.rb:3:in
<class:RouteSet>': undefined method
resources' for
ActionDispatch::Routing::RouteSet:Class
(NoMethodError)
Any suggestions much appreciated. At the present moment, the gem is nothing more than a very basic rails-generated 'frog' scaffold
Cheers,
- JB
#marcgg, I believe that's the syntax for a regular rails app, but I think he's talking about an engine.
#unclaimedbaggage, your engine/gem routes file should look like this:
Rails.application.routes.draw do |map|
resources :frogs
end
I made an example engine that touches on all the common setup issues I encountered when creating my first gem, you might find it helpful to reference:
http://keithschacht.com/creating-a-rails-3-engine-plugin-gem/
I'm not sure if I get why you're using a routeset. What file did you show? Did you try this:
YourApp::Application.routes.draw do |map|
resources :frogs
end
More info here: http://asciicasts.com/episodes/203-routing-in-rails-3
Just wanted to add an alternative here, as I'm not sure #Keith Schact is doing it the conventional way, this worked for me:
MyEngine::Engine.routes.draw do
resources :frogs
end
then in the application that requires the gem:
mount MyEngine::Engine => '/my_engine', :as => :some_namespace
The url you will get is then:
http://myserver.com/some_namespace/frogs

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