【问题标题】:How to write a path that includes two keys?如何编写包含两个键的路径?
【发布时间】:2017-08-13 22:18:17
【问题描述】:

我有两个模型,Artist 和 Song。每个艺术家都有_many Songs。我想为歌曲编写一个路径,在 url 中同时包含歌曲和艺术家。

我的路线:get 'songs/:artist/:id' => 'songs#song', as: 'song'

例如,“John Lennon”的“Imagine”的 URL 将是 /songs/John%20Lennon/Imagine

该网址工作正常。我不知道如何为它写一条路径。这些不起作用:

song_path(song: "Imagine", artist: "John Lennon")
song_path("John Lennon", "Imagine")

有人知道正确的语法吗?

【问题讨论】:

    标签: ruby-on-rails path routes rails-routing


    【解决方案1】:

    您使用的是 Rails 5 吗? resource_path 在 Rails 5 中已弃用。您仍然可以使用 link_to "song title", song_path(artist: "John Lennon", id: "Imagine")

    另外,请注意您在选项 1 中传递了键“song”,并使用“id”定义了路由。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 2012-03-08
      • 2021-08-14
      • 2013-07-26
      • 1970-01-01
      • 2017-03-08
      相关资源
      最近更新 更多