shortener icon indicating copy to clipboard operation
shortener copied to clipboard

Undefined Method short_url

Open Talha345 opened this issue 1 year ago • 5 comments

Hi, I am trying to generate a short url using the provided helper method but it gives me a undefined method `short_url' so I had to hack a similar method myself like:

  def short_url(shortened)
    Rails.application.routes.default_url_options[:host] + '/short_urls/' + shortened.unique_key
  end

I used the following route:

get '/short_urls/:id' => "shortener/shortened_urls#show"

Talha345 avatar Feb 20 '23 12:02 Talha345

Having the same issue here with the undefined method - is there a trick to getting this to work @jpmcgrath ? Can't seem to get it to work no matter what I try.

mchapman17 avatar Apr 12 '23 07:04 mchapman17

Hi @Talha345 and @mchapman17. I cannot reproduce this issue, trying various combinations of Ruby (2.5.1, 2.7.6, 3.0.1) and Rails (5.2.8, 7.0.7), and following the instructions in the README. It is difficult for me to say why the helper method is not being made available. Can you provide more information, like the ruby/rails versions you are using. Even better, can you please see if you can reproduce this issue in a fresh Rails application, and share the code?

jpmcgrath avatar Aug 25 '23 05:08 jpmcgrath

Yea I'm having the same problem. Can't access those helpers through views, controllers, or models. Anyone figure it out other than the hack?

This was my hacky solution: First I changed my route to be this which generated rails route helpers for "short_url" and "short_path"

get '/short/id' => 'shortener/shortened_urls#show', :as => :short

Then I had to add my own module helper to actually generate the links

module LinkShortener
  def self.short_url(url)
    link = ::Shortener::ShortenedUrl.generate(url)
    Rails.application.routes.url_helpers.short_url(id: link.unique_key)
  end
end

Definitely not as elegant as it could be since this is supposed to be built into the gem. I'm using this version

shortener (1.0.1)
      voight_kampff (~> 2.0)

ishields avatar Feb 06 '24 18:02 ishields

Have the same problem ruby(2.4.4) rails(5.2.8)

Merff avatar Feb 23 '24 10:02 Merff