there must obvious answer this, i'm @ loss.
i have rails app tracks sites. whatever reason, localhost:3000/sites leads index page. however, localhost:3000/sites/index leads show page.
why this?
below routes file:
rails.application.routes.draw resources :sites
below sites controller:
class sitescontroller < applicationcontroller def index end def show end end
in views/sites, there index.html.erb , show.html.erb files. former displays @ /sites, latter displays @ /sites/index (and /sites/show 1 expect).
thanks help!
update:
when rake routes, get:
prefix verb uri pattern controller#action sites /sites(.:format) sites#index post /sites(.:format) sites#create new_site /sites/new(.:format) sites#new edit_site /sites/:id/edit(.:format) sites#edit site /sites/:id(.:format) sites#show patch /sites/:id(.:format) sites#update put /sites/:id(.:format) sites#update delete /sites/:id(.:format) sites#destroy
these routes 1 expect, guess i'm surprised sites/index presumes index :id , therefore routes request show.
i suppose had never explicitly encountered behavior before.
there 7 basic actions in controller default, 2 of them matched name in url - (new
, edit
). these 2 html constructs, way of rendering form.
the other 5 (index, show, update, create, destroy) more basic routes , work display , modify resources. referenced two url patterns (the 2 patterns mentioned above - eg '/sites' , '/sites/:id'). differentiated method goes along request: (patch, post, get, delete). so, "/sites" used create
, index
. "/sites/:id" used show
, destroy
, , update
.
the action in controller not referenced url - url pattern , request method used call associated controller method.
in request "/sites/index", string "index" in url, route matches 1 has variable after "/sites/". :id
variable, not integer. since request get, first (and only) route match sites#show
. "index" value of params[:id]
passed show action.
Comments
Post a Comment