Giter VIP home page Giter VIP logo

Comments (7)

ronaldtse avatar ronaldtse commented on July 30, 2024

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?

from object_path.

skalee avatar skalee commented on July 30, 2024

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

from object_path.

skalee avatar skalee commented on July 30, 2024

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

from object_path.

PeterTKY avatar PeterTKY commented on July 30, 2024

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...

from object_path.

ronaldtse avatar ronaldtse commented on July 30, 2024

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

from object_path.

skalee avatar skalee commented on July 30, 2024
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

from object_path.

PeterTKY avatar PeterTKY commented on July 30, 2024

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.

from object_path.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.