I’m having trouble understanding routing. Plus I’m trying to understand the resources (and resource) key word in routes.rb.
I created home_controller.rb, and put “resources :home” in routes.rb. I ran rake routes, and i can see all that stuff, but I don’t know what to do with it. I simply want to display a page with a form, say index.html.erb, submit the form, do something in the controller, then display the exact same page again.
I think I want to invoke a PUT “/home”, or something, but I’m not sure what my href should look like in the erb page.
Then, what method in home_controller.rb will capture that submit?
Then, do I do a redirect back to index.html.erb, perhaps?
I’m trying not to use scaffolding or generators, so that I can understand what is going on. Any help appreciated. btw, rails 3.
Here is a quick run down of what you need to get started:
Every HTTP request in a rails app is routed to exactly one controller method. That controller method fulfills the request by either rendering some content or issuing a 302 redirect (which causes the browser to issue a different request).
A route in ROR describes how each HTTP request is mapped to each controller method. HTTP requests are mapped based on two attributes: the requested url and the HTTP verb (
GETPOSTPUTetc).When you do
resources :homesin yourroutes.rbfile your are telling Rails you have a controller calledHomesControlleryou are asking Rails to set up its conventional RESTful routes for that controller. These are as follows:HTTP
GETon/homesurlindexmethodHTTP
GETon/homes/1urlshowmethodid = 1HTTP
GETon/homes/newurlnewmethodHTTP
POSTon/homesurlcreatemethodnewform is submitted toHTTP
GETon/homes/1/editurleditmethodid=1HTTP
PUTon/homes/1urlupdatemethodeditform is submitted toid=1HTTP
DELETEon/homes/1urldeletemethodid=1