Create a small web project with crud addition, deletion, modification, and query operations using the method of creating Scaffold

Source: Internet
Author: User

[1] Create a rails project. We will name this project scaffoldtest.

[2] configure the config/database. yml file, that is, configure the database. Of course, you must first create the corresponding database

[3] use the generate tool or the following code on the command line after the new one is created:

$ Script/generate scaffold contact name: String Email: String

This is the core command, meaning to create a model layer object contact, which has two fields: Name (Data Type: string) and email (Data Type: string)

[3] If there is no accident, it should be successful. Go to the Controller, model, and view folders in the app folder and check that crud already exists, four template views are displayed: edit.html.erbw.index.html.erb;new.html.erb;show.html. ERB

[4] Run the $ rake DB: migrate command to run the code generated in the DB folder and create the table and its fields in the database.

[5] restart the server and check it in the browser!


The generated contacts_controller.rb is as follows:

Class contactscontroller <applicationcontroller <br/> # Get/contacts. XML <br/> def index <br/> @ contacts = contact. all <br/> respond_to do | format | <br/> format.html # index.html. ERB <br/> format. XML {render: XML =>@ contacts} <br/> end <br/> # Get/contacts/1 <br/> # Get/contacts/1.xml< br/> def show <br/> @ Contact = contact. find (Params [: Id]) <br/> respond_to do | format | <br/> format.html # show.html. ERB <br/> format. XML {render: xml => @ Contact} <br/> end <br/> # Get/contacts/New. XML <br/> def new <br/> @ Contact = contact. new <br/> respond_to do | format | <br/> format.html # new.html. ERB <br/> format. XML {render: xml => @ Contact} <br/> end <br/> # Get/contacts/1/edit <br/> def edit <br/> @ Contact = contact. find (Params [: Id]) <br/> end <br/> # post/contacts. XML <br/> def create <br/> @ Contact = contact. new (Params [: Contact]) <br/> respond_to do | format | <br/> If @ Contact. save <br/> FLASH [: Notice] = 'Contact was successfully created. '<br/> format.html {redirect_to (@ Contact)} <br/> format. XML {render: XML =>@ contact,: Status =>: created,: Location =>@ contact }< br/> else <br/> format.html {render: action => "new"} <br/> format. XML {render: xml => @ Contact. errors,: Status =>: unprocessable_entity} <br/> end <br/> # Put/contacts/1 <br/> # Put/contacts/1.xml< br/> def update <br/> @ Contact = contact. find (Params [: Id]) <br/> respond_to do | format | <br/> If @ Contact. update_attributes (Params [: Contact]) <br/> FLASH [: Notice] = 'Contact was successfully updated. '<br/> format.html {redirect_to (@ Contact)} <br/> format. XML {head: OK} <br/> else <br/> format.html {render: Action => "edit"} <br/> format. XML {render: xml => @ Contact. errors,: Status =>: unprocessable_entity} <br/> end <br/> # delete/contacts/1 <br/> # delete/contacts/1.xml< br/> def destroy <br/> @ Contact = contact. find (Params [: Id]) <br/> @ Contact. destroy <br/> respond_to do | format | <br/> format.html {redirect_to (contacts_url)} <br/> format. XML {head: OK} <br/> end <br/>


[PS] If this command fails, it may be because of a rails version problem. For details, refer to here:

Http://topic.csdn.net/u/20070513/22/5931d7c7-7451-4cc3-9dc5-ca3afbb0cae8.html? 241961724

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.