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

Make object_path/object_url generate path/url matching with the ones defined in Rails routes #1

Open
PeterTKY opened this issue Mar 22, 2018 · 7 comments

Comments

@PeterTKY
Copy link
Contributor

The object_path/object_url method should generate correct path/url as the routes defined in a Rails application. It should handle properly even if the routes are actually defined in a Rails engine and mounted in the application.

@ronaldtse
Copy link

It needs some kind of reference to connect a model with the desired Rails route, an object may often have more than one route (or just one unique route).

@skalee would you have time to help here?

@skalee
Copy link
Contributor

skalee commented Mar 22, 2018

I should be able to do this one, and EnMail in parallel.

@skalee
Copy link
Contributor

skalee commented Mar 22, 2018

@PeterTKY How does it differ from stock Rails' url_for/path_for method?

@PeterTKY
Copy link
Contributor Author

PeterTKY commented Mar 23, 2018

They should be quite similar. But I just tested a little bit in my Rails console. The url_for method seems not working in places other than ActionView.

class Tester
  include ActionView::RoutingUrlFor
end

Tester.new.url_for(Restaurant.last)

It will result in error:

NoMethodError: undefined method `_routes' for #<Tester:0x0000561f8de70088 @_routes=nil>

Unfortunately that's our major use case for object path...

@ronaldtse
Copy link

@skalee indeed as mentioned by @PeterTKY, the use case is to use object path outside of ActionView.

@skalee
Copy link
Contributor

skalee commented Mar 23, 2018

u = User.create
#=> #<User id: 1, email: nil, created_at: "2018-03-23 19:01:39", updated_at: "2018-03-23 19:01:39">

Rails.application.routes.url_helpers.polymorphic_url(u, :only_path => true)
#=> "/users/1"

Rails.application.routes.url_helpers.polymorphic_url(u, :host => "example.com")
#=> "http://example.com/users/1"

http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html#method-i-polymorphic_url

I'm not sure if this one-liner is worth extraction into a separate gem. Even though it's bit hackish.

cc @PeterTKY

@PeterTKY
Copy link
Contributor Author

There are actually some use cases can't be handled easily by polymorphic_url.

Example: We have three model classes, Restaurant::Chinese, Restaurant::Japanese and Restaurant::Western. In config/routes.rb:

namespace :restaurants do

  resources :chinese do
    # some routes for chinese
  end

  resources :japanese do
    # some routes for japanese
  end

  resources :western do
    # some routes for western
  end

end

In console:

Rails.application.routes.url_helpers.polymorphic_url([Restaurant::Chinese.last], only_path: true)
# => NoMethodError: undefined method `restaurant_chinese_url' for #<#<Class:0x000055a3a34ab220>:0x000055a3a34aa730>

We have to call:

Rails.application.routes.url_helpers.polymorphic_url([:restaurants_chinese], id: 1, only_path: true)

But then the :restaurants_chinese is not intuitive to have. We can't simply use the model instance in polymorphic_url like other cases.

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

3 participants