4

I am trying to access a url helper that I'm using in my event controller like this event_url(event).

Is there a way to access this in my event model?

Something like this?

event_url(self)

Im using rails v2.3.11.

Farhan Ahmad
  • 4,990
  • 5
  • 34
  • 67
  • Note that @Farhan uses Rails 2. ActionController::UrlWriter is deprecated in Rails 3, see some usefull links on http://apidock.com/rails/ActionController/UrlWriter. – Sergey Alekseev May 20 '13 at 20:03
  • Does this answer your question? [Can Rails Routing Helpers (i.e. mymodel\_path(model)) be Used in Models?](https://stackoverflow.com/questions/341143/can-rails-routing-helpers-i-e-mymodel-pathmodel-be-used-in-models) – Jared Beck Jan 31 '20 at 16:40

1 Answers1

5

Try adding this in your model

include ActionController::UrlWriter

You also need to add config lines to env configs: development.rb

config.action_controller.default_url_options = {:host => "localhost:3000"} 

production.rb

config.action_controller.default_url_options = {:host => "example.com"} 
Anatoliy Kukul
  • 1,894
  • 14
  • 24
  • I tried this but it did not work. undefined method `event_url' for # – Farhan Ahmad Dec 19 '12 at 20:22
  • 1
    try to include this `ActionController::UrlWriter` – Anatoliy Kukul Dec 19 '12 at 20:29
  • 2
    In Rails 3 it's possible to do it like this: `Rails.application.routes.url_helpers.event_url` I'm not sure if it works in Rails 2 – Anatoliy Kukul Dec 19 '12 at 20:29
  • Ok well something happened. I tried adding ActionController:UrlWriter and this is what I got back. Missing host to link to! Please provide :host parameter or set default_url_options[:host] – Farhan Ahmad Dec 19 '12 at 20:35
  • That's easy `event_url(self, :host => "localhost:3000")` – Anatoliy Kukul Dec 19 '12 at 20:37
  • 1
    It's example just to try if it works =) Really you need to add config lines to env configs: development.rb `config.action_controller.default_url_options = {:host => "localhost:3000"}` production.rb `config.action_controller.default_url_options = {:host => "example.com"}` – Anatoliy Kukul Dec 19 '12 at 20:43
  • @0A0D Ok. I'll remember that. AnatoliyKukul can you please update your answer with the correct answer from the comments. – Farhan Ahmad Dec 19 '12 at 20:49
  • I believe that should be `config.action_mailer.default_url_options` and NOT `config.action_controller.default_url_options`. Reference - http://api.rubyonrails.org/classes/ActionMailer/Base.html – Bongs Jul 17 '14 at 13:56