Defining routes problem

I defined some routes as follows:

resources :dashboard, only: [:destroy] do

collection do

get :timesheets

get :users

put :assign_admin

get :admins

put :remove_admin

match ‘/:year/:month/:day’, to: ‘dashboard#list_timesheets_by_start_date’,

as: ‘timesheets_by_date’,

constraints: { year: /\d{4}/, month: /\d{1, 2}/, day: /\d{1, 2}/ },

via: :get

end

end

Running ‘rake routes’ gives the following output:

timesheets_dashboard_index GET /dashboard/timesheets(.:format) dashboard#timesheets

users_dashboard_index GET /dashboard/users(.:format) dashboard#users

assign_admin_dashboard_index PUT /dashboard/assign_admin(.:format) dashboard#assign_admin

admins_dashboard_index GET /dashboard/admins(.:format) dashboard#admins

remove_admin_dashboard_index PUT /dashboard/remove_admin(.:format) dashboard#remove_admin

timesheets_by_date_dashboard_index GET /dashboard/:year/:month/:day(.:format) dashboard#list_timesheets_by_start_date {:year=>/\d{4}/, :month=>/\d{1, 2}/, :day=>/\d{1, 2}/}

dashboard DELETE /dashboard/:id(.:format) dashboard#destroy

The first question, - is it possible to make the defined routes more readable, for example remove the trailing part ‘dashboard_index’ and keep just ‘dashboard’ for example ?

The second question, the link to display time sheets by date is defined as follows in the view:

<%= link_to timesheet_week(timesheet), "/dashboard/#{date_path(timesheet.start_date)}" %> <%= timesheet.total_days %>

The helper ‘date_path’ is defined as follows in ApplicationHelper:

def date_path(date)

“#{date.year}/#{date.month}/#{date.day}”

end

def timesheet_week(timesheet)

“#{timesheet.start_date.to_formatted_s(:rfc822)} - #{timesheet.end_date.to_formatted_s(:rfc822)}”

end

But when clicking on the link for the week of April, 8th, I’m getting the error:

No route matches [GET] "/dashboard/2013/4/8"

Any idea how to fix that?

Thank you.

The first pont is fixed now: to use a singular route for DashboardController and remove the trailing ‘…dashboard_index’, I changed the routes
as follows:

resource :dashboard, :controller => “dashboard”, only: [:destroy] do
collection do
get :timesheets
get :users
put :assign_admin
get :admins
put :remove_admin
match ‘timesheets/:year/:month/:day’, to: ‘dashboard#list_timesheets_by_start_date’,
as: ‘timesheets_by_date’,
constraints: { year: /\d{4}/, month: /\d{1, 2}/, day: /\d{1, 2}/ },
via: :get
end
end

I still have the problem with matching the route to /dashboard/timesheets/:year/:month/:day.

Regards

The second point is also fixed, - the routes should be defined as follows:

match ‘/timesheets/:date’, to: ‘dashboard#list_timesheets_by_start_date’, via: :get,

constraints: { date: /\d{4}-\d{,2}-\d{,2}/ },

as: ‘timesheets_by_date’

An in the controller, one should just get the date from params:

def list_timesheets_by_start_date

unless params[:date]

@date = Date.today.beginning_of_week

end

@date ||= Date.strptime(params[:date], ‘%Y-%m-%d’)

timesheets = Timesheet.find_all_by_start_date(@date)

@timesheets = timesheets.paginate(page: params[:page])

render ‘selection’

end

Hope this helps.

Regards