root_path and root_url undefined

Hi all,

I've just started working on another RoR project after a couple of months away. I'm having trouble with named routes, specifically root_*

In previous apps I've used root_path and root_url without any problems, but in this project (2.3.2 - the same as my last project where I used it) it comes up undefined.

I've double and triple checking my routes.rb, and it all appears fine. I've defined map.root and removed index.html. I just can't think what could be causing this continuing problem.

I had upgraded this app to 2.3.5 and thought that might have been the cause. I googled around for deprecation notices or anything and eventually regressed rails back to 2.3.2 with no change.

I know I've probably missed something very simple, but it's completely eluding me right now. Any insights you guys could send my way would be appreciated.

Thanks

Matt

Not sure what is the issue is, I am running 2.3.4 and it works fine. I do have he map.root as the first entry although I don't think that will matter.

Can you send your routes.rb file?

Darian Shimy

Darian Shimy wrote:

Not sure what is the issue is, I am running 2.3.4 and it works fine. I do have he map.root as the first entry although I don't think that will matter.

Thanks for the reply Darian, I also have map.root as the first (uncommented) entry.

Can you send your routes.rb file?

My entire routes.rb is available at http://pastie.org/723159, but please don't laugh at my mess :smiley:

Darian Shimy

Thanks

Matt

Are you sure you restarted your server after introducing the map.root mapping ?

Thanks & Regards, Dhruva Sagar.

Also you try rake routes to see all your routes.

Satyajit Malugu wrote:

Also you try *rake routes * to see all your routes.

"root" appears as the first route in the list, and it looks properly formed:

http://pastie.org/723163

Can you post the error message and any info that appears in the log?

Thanks for the reply, I will get the information posted as soon as possible. I'm in hospital right now and have only just got internet access provided. As soon as I can get someone to turn on my dev machine I will be able to post the logs.

Oh and no flowers or cards please, everyone is so depressingly cheerful it's a wonder they are still alive :smiley:

Thanks

Matt

My apologies Rick, I didnt intent to send my reply only to you. Here is a copy for the list to peruse, including some extra information.

Since my last mail I also tried some of the other named routes I have defined, and all of them come up undefined as well. There is obviously something strange going on, and I'm not sure if it's me.

Can you post the error message and any info that appears in the log?

Ok here we go:

Error message and backtrace from the logs can be found here: http://pastie.org/729055

<pages_controller.rb> 1 class PagesController < ApplicationController 2 verify :method => :post, :only => [ 3 :update, :create, :destroy, :revert 4 ], :redirect_to => root_path 5 6 def show ....... </pages_controller.rb>

The routes.rb has been pastie'd in a previous post, but I will repeat the link here for reference (http://pastie.org/723159).

You can also see the result of "rake routes" here http://pastie.org/729464.

Please let me know if there is anything else I can provide.

Many thanks

Matt

Matt,

I believe in your controller you want to use:

:redirect_to => 'root_path'

Hi, thanks for the reply. Unfortunately that redirects me literally to http://myhost.tldroot_path

I just tried named routes from inside a view and they work as expected. It's only when I come to use the in a controller that they fail. I've also compared this project with a previous one I made that used named routes, and they appear to be used in exatly the same way.

Thanks

Matt

I think you must have something in your code (plugin perhaps?) that is mucking with the verify. Here's the environment I'm using to try do duplicate your problem:

About your application's environment Ruby version 1.8.7 (powerpc-darwin9.7.0) RubyGems version 1.3.5 Rack version 1.0 Rails version 2.3.2 Active Record version 2.3.2 Action Pack version 2.3.2 Active Resource version 2.3.2 Action Mailer version 2.3.2 Active Support version 2.3.2 Application root /Users/rick/matt Environment development Database adapter sqlite3 Database schema version 20091206142727

I did a 'rails matt' followed by 'script/generate scaffold Page title:string body:text' just to gin things up. I've also added

map.index '/', :controller => 'pages', :action => 'index'

to my routes.rb and removed public/index.html.

To be successful, my verify line must look like:

class PagesController < ApplicationController   verify :method => :post,         :only => [:edit, :destroy, :revert],         :add_flash => { :notice => 'Cannot do that...'},         :redirect_to => :index_path

or

class PagesController < ApplicationController   verify :method => :post,         :only => [:edit, :destroy, :revert],         :add_flash => { :notice => 'Cannot do that...'},         :redirect_to => 'index_path'

If I use

class PagesController < ApplicationController   verify :method => :post,         :only => [:edit, :destroy, :revert],         :add_flash => { :notice => 'Cannot do that...'},         :redirect_to => index_path

My app fails on the verify producing:

undefined local variable or method `index_path' for PagesController:Class

I would suggest you start a simple test app, like this, and then incrementally add to it any plugins you need.

Regardless, your use of :redirect_to => root_path (without quotes or colon) is what is producing the error.

Is your experience the same if you use :redirect_to => 'root_url' ?

How about :redirect_to => {   :controller => 'pages',   :action => 'show',   :name => 'HomePage'   }

Rick, many thanks for your reply. I believe I have sorted it now. If I use

:redirect_to => :root_url

it does appear to work as I expect. I realise that I wasn't using verify in my last project, and it seems to take slighly different arguments than redirect_to.

So for verify I will use the symbol :root_url but for redirect_to I will use the method root_url().

Many thanks for the help with this one, my head hasn't been clear enough to work it out for myself, I might have to lay off the morphine for a while :smiley:

Thanks

Matt

I use redirect_to root_url

Thanks & Regards, Dhruva Sagar.

You probably ought to save the morphine for when you're feeling better...