Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6774 (new defect)

Opened 1 year ago

Last modified 1 year ago

[PATCH] console's helper object doesn't work with #link_to

Reported by: theamazingrando Assigned to: Ulysses
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: minor Keywords: console link_to url_for
Cc:

Description

Trying to use link_to and link_to_remote on console's helper object results in a nil.url_for error. Using edge rails.

 $ ./script/console
Loading development environment.
>> helper.human_size(5000000000)
=> "4.7 GB"
>> helper.link_to 'test', :action => 'test'
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.url_for
        from ./script/../config/../config/../vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:27:in `send'
        from ./script/../config/../config/../vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:27:in `url_for'
        from ./script/../config/../config/../vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:75:in `link_to'
        from (irb):2
>> app.url_for :controller => 'graph'
=> "http://www.example.com/graph"

Attachments

console_helper_patch.diff (419 bytes) - added by Chris Wanstrath on 12/07/06 04:46:43.
Set @controller variable on helper to fix link_to calls

Change History

12/06/06 18:25:14 changed by theamazingrando

The issue is that @controller (line 27 of url_helper.rb)

I was able to get closer by defining a controller for helper object, but resulted in another error:

>> def helper.controller= c
>> @controller = c
>> end
=> nil
>> helper.controller = @controller
=> #<ApplicationController:0x2b3b64264bf0>
>> helper.controller
=> #<ApplicationController:0x2b3b64264bf0>
>> helper.link_to( 'test', :action => 'test')
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.rewrite
        from ./script/../config/../config/../vendor/rails/actionpack/lib/action_controller/base.rb:519:in `url_for'
        from ./script/../config/../config/../vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:27:in `send'
        from ./script/../config/../config/../vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:27:in `url_for'
        from ./script/../config/../config/../vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:75:in `link_to'
        from (irb):22

This results from @url on line 519 of action_controller/base.rb being nil.

12/06/06 19:45:50 changed by bitsweat

  • keywords set to console link_to url_for.
  • owner changed from core to Ulysses.

12/07/06 04:46:43 changed by Chris Wanstrath

  • attachment console_helper_patch.diff added.

Set @controller variable on helper to fix link_to calls

12/07/06 04:47:48 changed by Chris Wanstrath

  • summary changed from console's helper object doesn't work with #link_to to [PATCH] console's helper object doesn't work with #link_to.

This fixes it. Not sure if ApplicationController.new could work without some hackery -- the nil in question (in the bug report) is @url, which only seems to get set during a #process call with a legit #params and #request. Easier to do what #app does.

01/12/07 01:01:55 changed by nzkoz

  • owner changed from Ulysses to nzkoz.
  • status changed from new to assigned.

01/12/07 01:02:24 changed by nzkoz

  • owner changed from nzkoz to Ulysses.
  • status changed from assigned to new.

wrong ticket

05/22/07 02:41:36 changed by nik.wakelin

Hi,

Wouldn't it be better to do:

helper.instance_variable_set("@controller", app)

Instead? Might aswell use the instance of Integration::Session that is already lying around :)

Anyway, this patch totally doesn't work now. As of the time of writing , I get this error:

>> helper.link_to '/'
ActionController::RoutingError: Need controller and action!
        from /Users/nik/projects/rails_patches/6774/vendor/rails/actionpack/lib/action_controller/routing.rb:1305:in `generate'
        from /Users/nik/projects/rails_patches/6774/vendor/rails/actionpack/lib/action_controller/url_rewriter.rb:113:in `rewrite_path'
        from /Users/nik/projects/rails_patches/6774/vendor/rails/actionpack/lib/action_controller/url_rewriter.rb:93:in `rewrite_url'
        from /Users/nik/projects/rails_patches/6774/vendor/rails/actionpack/lib/action_controller/url_rewriter.rb:70:in `rewrite'
        from /Users/nik/projects/rails_patches/6774/vendor/rails/actionpack/lib/action_controller/integration.rb:203:in `url_for'
        from /Users/nik/projects/rails_patches/6774/vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:70:in `send'
        from /Users/nik/projects/rails_patches/6774/vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:70:in `url_for'
        from /Users/nik/projects/rails_patches/6774/vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:138:in `link_to'
        from (irb):16

Anyone got any ideas on how to fix it? I could dig around but I've totally never had t use link_to on the helper object.... (why not just use app.url_for?)