ruby on rails - Better way of building urls -


i have route configured this:

get "calendar/:year/:month" => "calendar#month", as: :calendar_month 

to create links "next" month , "previous" month, need use in template:

<%= link_to "next", calendar_month_path(year: @date.next_month.year, month: @date.next_month.month) %> 

or bit simpler:

<%= link_to "previous", calendar_month_path(@date.prev_month.year, @date.prev_month.month) %> 

both feel bit verbose.

is there way take advantage of @date.prev_month/@date.next_month return object (date) has methods respond defined in route params (:year, :month)?

something similar to:

<%= link_to "previous", calendar_month_path(@date.prev_month) %> 

would ideal solution, doesn't work.

how deal similar situations?

thank advice!

you can add method in helpers convert date calendar_month_path:

def path_for_date(d)   calendar_month_path(d.year, d.month) end 

which means links are:

link_to("previous", path_for_date(@date.prev_month) link_to("next", path_for_date(@date.next_month) 

depending on other routes may want name bit better have idea.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -