configuring rails3 route with multiple parameters
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
精彩评论