【问题标题】:Matching routes that looks something like `pages/1/2/3/4/mass_edit`匹配类似于 `pages/1/2/3/4/mass_edit` 的路由
【发布时间】:2013-02-22 22:14:00
【问题描述】:

我希望能够匹配 array 的 id 而不是单个 id。

我在路由文件中有这个。

resources :pages do
  member do
    get "mass_edit"
  end
end

但正如你所知,它只支持pages/1/mass_edit 格式的路由。

我需要路由来支持pages/1/2/3/mass_edit 或 pages/1/2/3/4/10/111/1111/mass_edit` 等格式。像这样的 URL 是通过 mass_edit_page_path([1,2,3]) 生成的。

有没有办法做到这一点?

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

    也许这可以解决问题:

    resources :pages do
      match "mass_edit/:ids" => 'pages#mass_edit'
    end
    

    然后在你的控制器中:

    def mass_edit
      parse_ids
      # do your edit thing using @ids
    end
    
    def parse_ids
      @ids = params[:ids].split(',').map(&:to_i).uniq
    end
    

    网址:

    /pages/mass_edit/1,5,3,24,63
    

    【讨论】:

    • 仅从 :ids 更改为 :id。完美的答案。
    猜你喜欢
    • 1970-01-01
    • 2021-07-04
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多