【问题标题】:configuring rails3 route with multiple parameters使用多个参数配置 rails3 路由
【发布时间】:2011-11-10 16:53:41
【问题描述】:

我有什么

match "/home/markread/:id" => "books#markread"

def markread
  #mark params[:id] as read
end

我想要什么

如果我想传递另一个参数以使 url 看起来像 /home/markread/1/didread=read/home/markread/1/didread=unread

所以我的方法会变成

def marked
  #mark params[:id] as params[:didread]
end

问题

我的routes.rb 应该是什么样子才能实现这一目标?

【问题讨论】:

    标签: ruby-on-rails-3 routes url-routing


    【解决方案1】:

    在 Rails 4 中,您将拥有:

        resources :home, only: :none do
          get 'markread/:another', action: :markread, on: :member
        end
    

    GET /home/:id/markread/:another(.:format) /home#markread

    【讨论】:

      【解决方案2】:

      使用 'as' 选项为路由命名,并根据需要传递可选参数。

      例如:

      match "/home/markread/:id" => "books#markread", :as => 'markread'
      

      这将为您提供markread_pathmarkread_url 之类的助手。你可以通过parameters like markread_path(:id => 1, :other => 'value' ...)

      您需要在控制器中检查该操作是否传递了特定参数。 Rails Doc.

      【讨论】:

      • 从您提供的示例中...我还能传递多个参数吗?
      • 是的。您可以传入任意数量的参数。决定你想在控制器中对它们做什么。
      【解决方案3】:

      换成

      match "home/markread/:id/used=:used" => "books#markread"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-19
        • 1970-01-01
        • 1970-01-01
        • 2020-09-10
        • 2011-10-12
        • 2020-05-31
        相关资源
        最近更新 更多