使用多个参数配置rails3路由

时间:2011-09-05 14:55:16

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

我有什么

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

转到

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

我想要什么

如果我想传递另一个参数以便网址看起来像什么 /home/markread/1/didread=read/home/markread/1/didread=unread

所以我的方法将改为

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

问题

我的routes.rb对我来说应该是什么样的?

3 个答案:

答案 0 :(得分:5)

如何换到

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

答案 1 :(得分:3)

使用'as'选项为路径命名,并将可选参数传递给任意数量。

例如:

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

这会为您提供markread_pathmarkread_url等帮助。您可以传递parameters like markread_path(:id => 1, :other => 'value' ...)

无论是否传递特定参数,您都需要在控制器中执行该操作的检查。 Rails Doc.

答案 2 :(得分:1)

在rails 4中你将拥有:

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

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

相关问题