Anthony
Anthony

Reputation: 35938

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?

Upvotes: 3

Views: 7239

Answers (3)

Bruno Casali
Bruno Casali

Reputation: 1380

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

Upvotes: 1

Syed Aslam
Syed Aslam

Reputation: 8807

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.

Upvotes: 3

Omnipresent
Omnipresent

Reputation: 30384

How about just changing to

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

Upvotes: 5

Related Questions