I added in the route file:
map.show_book "/show_book/:name/year/:year", :controller => "book", :action => "show_version"
I also added:
map.show_book "/show_book/:name", :controller => "book", :action => "show_version"
to show the latest book without specifying the year.
But it doesn't work, it cannot find the route in "show_book/NAME" if I don't pass the year.
Do you have some ideas why it doesn't work ?
THANKS !
PS. I know that I can use year as parameter with "?year=XXXX", but I want to use the year as a part of the URL
To add in an optional path parameter in React Router, you just need to add a ? to the end of the parameter to signify that it is optional. And then in your component you could access the GET parameters using the location.search prop that is passed in by React Router.
A method that contains optional parameters does not force to pass arguments at calling time. It means we call method without passing the arguments. The optional parameter contains a default value in function definition. If we do not pass optional argument value at calling time, the default value is used.
To implement the optional parameter first you need to add System. Runtime. InteropServices namespace in your program, then creates an optional parameter using the Optional keyword enclosed in square brackets before the definition of the parameter in the method. The default value of OptionalAttribut is zero.
Put the optional parts between parenthesis:
map.show_book "/show_book/:name(/year/:year)", :controller => "book", :action => "show_version"
and remove the second route.
Update
The above answer is only for rails 3 and above. Inverting the two routes definitions fixed the problem (see Alessandro's comment below).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With