After several days, the Ror environment was finally ready. In order to prevent future forgetting and minimize detours, we made a special record
========================================================== =====
Installation Process
1. Install rubyinstaller-1.9.2
2. install and execute gem install rails
3. Download dev kit and decompress it to the ruby installation directory.
4. Install Netbeans 6.9.1
5. Enable Netbeans to configure Ruby's gem key mysql2 0.2.6 so that ror supports mysql
6. Install ruby_debug_ide. (The error has been reported)
Development Process
1. Create a project (Blog)
2. Configure database. yam
3. Run the rake task db: create (the database will be created in mysql based on database. yam, without manual database creation)
4. Start the service and open the page to check whether the environment variables are correctly displayed.
5. Create the controller home index
6. Open the service http: // 127.0.0.1: 3000/home/index to check whether the service is normal.
7. Change the default homepage (1. Delete public \ index.html 2. Change root in routes: to => "home # index ")
8. Create a scaffold model (generate scaffold Post name: string title: string content: text)
9. Execute the rake task and synchronize the new data model to the database. rake db: migrate
10. Add the link to the scaffold on the homepage (modify it in views \ home \ index.html. erb
<H1> Hello, Rails! </H1> <% = link_to "My Blog ",
Posts_path%>
Pay attention to the posts_path in it, as agreed in writing
11. Open the homepage http: // 127.0.0.1: 3000/and you will see a hyperlink to the blog.
12. Connect to the database for CURD testing.
13. Add verification information to the model file.
class Post < ActiveRecord::Base validates :name , :presence => true validates :title , :presence => true , :length => { :minimum => 5 } end |
Presence indicates whether it can be empty, and length indicates the length of the field
14. Test whether verification information is displayed.
15. Create a common model Comment (post Comment)
$ Rails generate model Comment commenter: string body: text post: references
Note:Post: references
16. A one-to-many relationship is automatically generated in the Post model.
class Comment < ActiveRecord::Base belongs_to :post end |
17. Run rake to synchronize the new data model to the database. rake db: migrate
18. Modify the Post model to add a one-to-multiple ing.
class Post < ActiveRecord::Base validates :name , :presence => true validates :title , :presence => true , :length => { :minimum => 5 } has_many :comments end |
19. Configure Comments ing in route (to be studied)
resources :posts do resources :comments end |
20. Create a controller
$ rails generate controller Comments |
21. Modify views/posts/show.html. erb and edit Comment.
<
p
class
=
"notice"
>
<%=
notice
%>
</
p
>
<
p
>
<
b
>Name:</
b
>
<%=
@post
.name
%>
</
p
>
<
p
>
<
b
>Title:</
b
>
<%=
@post
.title
%>
</
p
>
<
p
>
<
b
>Content:</
b
>
<%=
@post
.content
%>
</
p
>
<
h2
>Add a comment:</
h2
>
<%=
form_for([
@post
,
@post
.comments.build])
do
|f|
%>
<
div
class
=
"field"
>
<%=
f.label
:commenter
%>
<
br
/>
<%=
f.text_field
:commenter
%>
</
div
>
<
div
class
=
"field"
>
<%=
f.label
:body
%>
<
br
/>
<%=
f.text_area
:body
%>
</
div
>
<
div
class
=
"actions"
>
<%=
f.submit
%>
</
div
>
<%
end
%>
<%=
link_to
'Edit Post'
, edit_post_path(
@post
)
%>
|
<%=
link_to
'Back to Posts'
, posts_path
%>
|
22. Modify commentcontroller
class CommentsController < ApplicationController def create @post = Post.find(params[ :post_id ]) @comment = @post .comments.create(params[ :comment ]) redirect_to post_path( @post ) end end |
23. Write it here first
Specific reference http://guides.rubyonrails.org/getting_started.html