简体   繁体   中英

simple rails routing using resources

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

  1. http://guides.rubyonrails.org/getting_started.html#rest
  2. http://guides.rubyonrails.org/routing.html

Here is a quick run down of what you need to get started:

  1. 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).

  2. 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).

  3. 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

  • mapped to the index method
  • usually used to list all of the homes records

HTTP GET on /homes/1 url

  • mapped to the show method
  • usually used to show the home record with id = 1

HTTP GET on /homes/new url

  • mapped to the new method
  • usually used render a form for creating a new home record

HTTP POST on /homes url

  • mapped to the create method
  • where the new form is submitted to
  • used to create a new home record
  • usually responds with a redirect

HTTP GET on /homes/1/edit url

  • mapped to the edit method
  • usually used to render a form for editing home record with id=1

HTTP PUT on /homes/1 url

  • mapped to the update method
  • where the edit form is submitted to
  • used to edit the record with id=1
  • usually responds with a redirect

HTTP DELETE on /homes/1 url

  • mapped to the delete method
  • usually used to destroy the record with id=1
  • usually responds with a redirect

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM