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.
it's too basic. please refer to the rails doc which is more detailed than our answers. see
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 ( GET
POST
PUT
etc).
When you do resources :homes
in your routes.rb
file your are telling Rails you have a controller called HomesController
you are asking Rails to set up its conventional RESTful routes for that controller. These are as follows:
HTTP GET
on /homes
url
index
method HTTP GET
on /homes/1
url
show
method id = 1
HTTP GET
on /homes/new
url
new
method HTTP POST
on /homes
url
create
method new
form is submitted to HTTP GET
on /homes/1/edit
url
edit
method id=1
HTTP PUT
on /homes/1
url
update
method edit
form is submitted to id=1
HTTP DELETE
on /homes/1
url
delete
method id=1
Put simply, the resources keyword specifies that you have an object (like a user, article, post, comment, etc) that you want to have Create Read Update and Delete actions on. It specifies 7 actions, known as RESTful actions, that let you list the objects (index), update (edit, update), create (new, create), or delete (destroy).
This corresponds to 7 actions in your controller and 4 view files (create, update and delete won't have their own view typically).
The rake routes spits out the routes, their paths, and their path helpers. So you can link to the index action/page by
<%= link_to 'New Article', new_article_path %>
Or various other uses that keep you from having to hard code paths that may change with your design.
Sounds, by your design, that you don't need all 7 actions. You just need to show a form and create the model object based on what's input. So you might do
resources :home, only => [:new, :create]
Where you'd show the form on new and insert the record into the DB on create.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.