开发者

configuring rails3 route with multiple parameters

开发者 https://www.devze.com 2023-04-02 07:31 出处:网络
What I have match \"/home/markread/:id\" => \"books#markread\" goes to def markread #mark params[:id] as read

What I have

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

goes to

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

What I want

What If I want to pass another parameter so that urls looks like /home/markread/1/didread=read or /home/markread/1/didread=unread

开发者_开发技巧

so my method will change to

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

Question

what should my routes.rb look like for me to achieve this?


How about just changing to

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


Give the route a name using the 'as' option and pass the optional parameters as many you want.

For example:

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

This will give you helpers like, markread_path and markread_url. You can pass the parameters like markread_path(:id => 1, :other => 'value' ...)

You need to do the checks in the controller for that action whether a particular parameter is passed or not. Rails Doc.


In rails 4 you will have:

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号