开发者

How can I properly use Rails routes in a service class?

I'm trying to use Rails routes outside the Rails commons classes, like controllers, views and even models. I was able to make it work with this code:

class SomeSer开发者_开发问答vice
  include Rails.application.routes.url_helpers
  default_url_options[:host] = 'localhost'

  def myMethod
    my_resource_url(:id => 1)
  end
end

SomeService#myMethod works as expected in my tests classes, returning url like: http://localhost/myresource/1

But when I run rails server I can't get the actual host and port. How can I solve this?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜