Use of Ruby on Rails instantrails

Source: Internet
Author: User
Tags ruby on rails

Next, let's get started with Ruby on Rails (ROR ).
First, we need to quickly configure the development environment of Ruby on Rails.
1. Download InstantRails-2.0-win.zip
Can go to official download, http://rubyforge.org/frs? Group_id = 904
You can also go to/download/tools/InstantRails-2.0-win.zip to change
/Download/tools/InstantRails-2.0-win.7z downloading
2. After downloading, decompress the package.
If you download from/download/tools/InstantRails-2.0-win.7z
Enter the decompressed password www.17rumen.com.
There are many files. Wait a moment and the software does not need to be installed. You can decompress the package.
We recommend that you use instantrails as the ROR development environment.
Instantrails.exe run
The green light on Apache MySQL is displayed, indicating that the service is running normally.

 

Http: // localhost/
Http: // localhost/MySQL/in phpMyAdmin to manage MySQL Server
(MySQL Default User is root without password)

Select Configure> Windows Hosts file
Join
127.0.0.1 www.mycookbook.com
127.0.0.1 typo

Select Rails Applications> Manage Rails Applications...
Then select cookbookc and press "Start scgi" button.
Ingress into http://www.mycookbook.com/inbound site

Before playing with rails, you should first understand Ruby:
In Windows OS, enter CD/instantrails/Ruby/bin
Example row: Ruby-V
View Ruby version
Primary row: IRB
Enter the ruby shell to run the compute program.
Rows: IRB -- simple-Prompt
Then print ('joeyta ') is used to view the output

Build master C:/joeyta. RB, content:
Print ("My name is:") # No
Puts "joeyta" # outputs of puts are automatically generated.
Print "What is your name? /N "# Double Quote can solve the javasescape character
Puts 'Peter Chan/N' # single quote directly outputs all characters
Print '1 + 1 = '; puts 1 + 1 # Seek back 1 + 1 = 2
Puts "ABC" = "ABC" # returns true
A = "AB"; B = "AB"
Puts "AB". eql? "AB" # returns true to determine the actual limit value.
Puts "AB". Equal? "AB" # False when callback is returned, and the test location is determined.
Puts 10> 50 # reject false
Puts "ABCD". Index ('C') # returns 2

Name = "joeyta ";
Printf ("Name: % s/n", name) # printf can be formatted
Printf ("Is it joeyta? % S/n ", (name = 'joeyta '? 'Yes': 'no '))

Number = gets. to_ I # After gets is retrieved, the value of to_ I is converted into a number, giving the number
Puts number

If number = 1 # If table limit
Puts 'partition into 1'
Elsif number = 2
Puts 'puts into 2'
Else
Puts 'values cannot be 1 or 2'
End

Unless number = 1 # When number is not 1, it is true.
Puts 'output cannot be 1'
Else
Puts 'outputs to 1'
End

Case number # case table limit type
When 1
Puts 'case 1'
When 2
Puts 'Case 2'
Else
Puts 'case 1, 2'
End

For I in 0 .. 2
Print I, "/N"
End

For element in [0.2, 4, 'joeyta ']
Print "# {element}/t (# {element. Class})/n"
End

(0 .. 2). Each {| I | puts I}
(9 .. 12). Each do | I | puts I end
2. Times {puts "joeyta "}
3. Times do | I | puts "Peter" End

J = 1000
Begin
J-= 1
Puts J
If J = 997
Break
End
End while j> = 995 # can also use

(1 .. 5). Each do | num |
Print num
If num = 4
Break # You can also use redo failover. Next, retry to retry failover.
End
End

Values = [2, 4, 6, 8, 10]
Values. length. Times do | index |
Print values [Index], ""
End

Ary = array. New (3). Fill {"foo "}
Ary [0]. Replace "bar"
P ary

Rows
C:/instantrails/Ruby/bin> Ruby C:/joeyta. Rb

After playing Ruby, you can now play with rails:
C:/instantrails/Ruby/bin> rails C:/instantrails/rails_apps/mybook
In the C:/instantrails/rails_apps/object, the application of the primary mybook and the related cases will be created.

Rows
Ruby C:/instantrails/rails_apps/mybook/script/Server
Or to instant rails> I> rails Application> Manage Rails Applications
Select mybook and press "Start with webrick"
The website will be released.
Upload http: // 127.0.0.1: 3000/

Ruby C:/instantrails/rails_apps/mybook/script/generate controller mytest
C:/instantrails/rails_apps/mybook/APP/controllers/my_test_controller.rb
Class mytestcontroller <applicationcontroller
Def Index
Render_text "Hello World"
End
End

Export http: // 127.0.0.1: 3000/my_test/To see hello World

Please wait until C:/instantrails/rails_apps/mybook/APP/controllers/my_test_controller.rb
Class mytestcontroller <applicationcontroller
Def Index
Render_text "Hello World"
End
Def hello
Render_text "Hello rails"
End
End

Export http: // 127.0.0.1: 3000/my_test/hello to see hello rails

Create data sources:
Enter http: // localhost/MySQL/

Rows:
Create Database mybook;
Create Table books (
Id int (11) auto_increment primary key,
Title varchar (100 ),
Description text,
Buydate date)

Modify C:/instantrails/rails_apps/mybook/config/database. yml
(Yaml configuration benchmark, can be used to test the http://www.yaml.org/and http://www.ruby-doc.org/core/classes/YAML.html)
Development:
Adapter: MySQL
Database: mybook
Username: Root
Password:
HOST: localhost
Test:
Adapter: MySQL
Database: mybook
Username: Root
Password:
HOST: localhost
Production:
Adapter: MySQL
Database: mybook
Username: Root
Password:
HOST: localhost

Rows
Ruby C:/instantrails/rails_apps/mybook/script/generate Model Book
Book. RB will be generated in C:/instantrails/rails_apps/mybook/APP/models.
Rails intelligently maps books to the books table of MySQL.
(When creating a model book, the book will be mapped to the English book talbe, that is, the books table)

Rows
Ruby C:/instantrails/rails_apps/mybook/script/generate controller book
C:/instantrails/rails_apps/mybook/APP/controllers/book_controller.rb
Class bookcontroller <applicationcontroller
Scaffold: book # scaffold: Book generates the crud generation Sequence
End

Ingress into http: // 127.0.0.1: 3000/book/New
Unexpectedly, the UI was generated, allowing users to add and modify the partition data to MySQL books table.

C:/instantrails/rails_apps/mybook/APP/controllers/book_controller.rb
Class bookcontroller <applicationcontroller
Scaffold: Book
Def list
End
When a template is entered in http: // 127.0.0.1: 3000/book/new, the missing template appears.

New
C:/instantrails/rails_apps/mybook/APP/views/book/list. rhtml content
<HTML>
<Head>
<Title> all books </title>
</Head>
<Body>
<H1> online mybook-all books <Table border = "1">
<Tr>
<TD width = "80%"> <p align = "center"> <I> <B> book </B> </I> </TD>
<TD width = "20%"> <p align = "center"> <I> <B> date </B> </I> </TD>
</Tr>
<% @ Books. Each do | book | %>
<Tr>
<TD> <% = link_to book. Title,: Action => "show",: Id => book. ID %> </TD>
<TD> <% = book. buydate %> </TD>
</Tr>
<% End %>
</Table>
<P> <% = link_to "create new book",: Action => "new" %> </P>
</Body>
</Html>

Modify C:/instantrails/rails_apps/mybook/APP/controllers/book_controller.rb
Class bookcontroller <applicationcontroller
Scaffold: Book
Def list
@ Books = book. find_all
End
End

When you load http: // 127.0.0.1: 3000/book/List, a custom list template is displayed.

Enter http: // localhost/MySQL/
Use mybook;
Create Table categories (
Id int (11) auto_increment primary key,
Name varchar (50)
);
Alter table books add category_id int (11) not null after description;
Insert into 'categories 'values (1, 'Statement ');
Insert into 'category' values (2, 'sci-fi ');
Insert into 'categories 'values (3, 'manipulation ');
Insert into 'books 'values (1, 'tianhai', 'tianhai Yiyun ', 1, '2017-04-28 ');
Insert into 'books 'values (2, 'good analytics', 'good analytics', 2, '2017-04-29 ');

Rows
Ruby C:/instantrails/rails_apps/mybook/script/generate model category
Ruby C:/instantrails/rails_apps/mybook/script/generate controller category

Modify C:/instantrails/rails_apps/mybook/APP/model/book. Rb
Class book <activerecord: Base
Belongs_to: Category
End

Modify C:/instantrails/rails_apps/mybook/APP/model/category. Rb
Class Category <activerecord: Base
Has_books: Books
End

Modify C:/instantrails/rails_apps/mybook/APP/controllers/book_controller.rb
Class bookcontroller <applicationcontroller
Scaffold: Book
Def list
@ Books = book. find_all
End
Def Edit
@ Book = book. Find (@ Params ["ID"])
@ Categories = category. find_all
End
End

Added C:/instantrails/rails_apps/mybook/APP/views/book/list. rhtml content
<HTML>
<Head>
<Title> edit book </title> <Body>
<H1> edit book <Form action = "../update" method = "Post">
<Input id = "book_id" name = "Book [ID]" size = "30" type = "hidden" value = "<% = @ book. ID %>"/>
<P> <B> title </B> <br>
<Input id = "book_title" name = "Book [title]" size = "30" type = "text" value = "<% = @ book. title %> "/> </P>
<P> <B> description </B> <br>
<Input id = "book_description" name = "Book [description]" size = "30" type = "text" value = "<% = @ book. description %> "/> </P>
<P> <B> category: </B> <br>
<Select name = "Book [category_id]">
<% @ Categories. Each do | category | %>
<Option value = "<% = category. id %> "<% = 'selected' if category. id = @ book. category. id %> <% = category. name %>
</Option>
<% End %>
</SELECT> </P>
<Input type = "Submit" value = "Update"/>
</Form>
<A href = "/book/show/<% = @ book. id %> "> show </a> | <a href ="/book/List "> back </a>
</Body>
</Html>

 

Related Article

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.