Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined Method short_url #156

Open
Talha345 opened this issue Feb 20, 2023 · 6 comments
Open

Undefined Method short_url #156

Talha345 opened this issue Feb 20, 2023 · 6 comments

Comments

@Talha345
Copy link

Talha345 commented Feb 20, 2023

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"

@mchapman17
Copy link

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.

@jpmcgrath
Copy link
Owner

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?

@ishields
Copy link

ishields commented Feb 6, 2024

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)

@Merff
Copy link

Merff commented Feb 23, 2024

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

@jtjanaka
Copy link

I got the same issue for short_url, I'm calling it from a mailer view and rspec threw the error. Any of these 2 works for me:

module ApplicationHelper
  include Shortener::ShortenerHelper
  ...
  # I have a wrapper helper method that call short_url
  def wrapped_short_url(object)
    short_url(object.path, expires_at: object.expires_at)
  end
end

or

class ApplicationMailer < ActionMailer::Base
  helper :application
  helper 'shortener/shortener'
  ...
end

I chose the later because it only affects my mailer, basically you just need to include the helper if the method is missing. I'm using Rails 7.0.8.4.

@mame2024
Copy link

mame2024 commented Oct 4, 2024

Same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants