1. Use rails3.0 to create a simple website

Source: Internet
Author: User

First, open cmd and enter some commands in it to create a site.

rails new asshole

For example, asshole indicates the name of the project to be started, and you can change the name to another one. Then, an asshole Project Document will be created in the current directory,

Create a database again. You can call the following command to create a database.

rails generate scaffold Product title:string description:text image_url:string price:decimal

Of course, you need to understand what this means. rails generate scaffold is a command, Product is the table name, and the following title is the field name, followed by the colon is the type, so here in turn several new fields, including title and description, are created for the table "Product", and then the command is successfully generated. However, the database has not yet been generated and is only being configured,

Now I am not satisfied with the price field. I want to add some constraints. What I know is that the price unit should be a reasonable range. So I want to request that the length of the type change should be 8 characters long. to meet this extra demand, I decided to modify the above configuration, then open the db folder in the project directory, then enter the migrate directory, there will be A file like 20111012024954_create_products.rb may be different, but such a name may appear after it is opened in the editor,

 1 class CreateProducts < ActiveRecord::Migration
2 def self.up
3 create_table :products do |t|
4 t.string :title
5 t.text :description
6 t.string :image_url
7 t.decimal :price
8
9 t.timestamps
10 end
11 end
12
13 def self.down
14 drop_table :products
15 end
16 end

Modify row 7th and change it

      t.decimal :price, :precision => 8, :scale => 2

In fact, the command just entered generates the database generated in winter and winter, and so on. The database is built on this blueprint. Well, execute the database Generation Command.

rake db:migrate

Enter

rails server

You can start the rails server and use the default localhost: 3000 domain names are used to access your new website. This command not only generates databases, but also related pages, magic, this is rails,

Enter localhost: 3000/products to access the page you just generated. In fact, the command to start the rails server can be abbreviated as rails s.

Now, you can try a new rails page, add products or something, but you will find that this winter style is very simple, so if you want to modify the page style, you can open the project directory, modify the file/app/views/products/_form.html. erb, this is actually the template page of the input data table, and then modify it. For the CSS style table of this project, it is placed in public/stylesheets by default, and there is a directory of js and image next to it, it should be hard to recognize that images and js are all placed in the corresponding directory. Note that if you upgrade to rails3.1 later, these three folders will be merged and put in a folder named ass header. remember,

The configuration of CSS is also emphasized here. This configuration file is stored in the/app/views/layouts/application.html. erb file and opened.

 1 <!DOCTYPE html>
2 3 4 <title>Depot</title>
5 <%= stylesheet_link_tag :all %>
6 <%= javascript_include_tag :defaults %>
7 <%= csrf_meta_tag %>
8 9 <body>
10 <%= yield %>
11 </body>
12

The first row is, the default value is all, that is, to load all css style sheets. If you do not want to do this, you can change the name of the corresponding style sheet,

In fact, at this step, you should know that the rails address for storing the template page is placed in this/app/views/. Of course, the names of each sub-item are connected. In this case, the sub-file folder products is opened and index.html is also displayed. erb files are all template pages. If you want to view the pages of this webpage, you can refer to these files and css files,

As mentioned above, the data you entered is input through the generated products Page. Can you change the data directly in the database? This is of course, but rails provides a safer method. open/db/seeds in the project directory through the configuration file. rb: This file determines the configuration of data that you add to the database. In fact, you can write an SQL statement, open it, and enter some product data, such enter a data file with the title "fuck" and description as "I'm happy,

 1 Product.create(:title => 'fuck',
2 :description =>
3 %{<p>
4 i'm happy.
7 </p>},
8
9 :image_url => '/images/ruby.jpg',
10 :price => 49.50
11 )

Save the preceding statement and run the following statement.

rake db:seed

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.