![ruby on rails 創(chuàng)建一個博客項目_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/8/fd71becd-9b03-4b91-8878-c0d14752079b/fd71becd-9b03-4b91-8878-c0d14752079b1.gif)
![ruby on rails 創(chuàng)建一個博客項目_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/8/fd71becd-9b03-4b91-8878-c0d14752079b/fd71becd-9b03-4b91-8878-c0d14752079b2.gif)
![ruby on rails 創(chuàng)建一個博客項目_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/8/fd71becd-9b03-4b91-8878-c0d14752079b/fd71becd-9b03-4b91-8878-c0d14752079b3.gif)
![ruby on rails 創(chuàng)建一個博客項目_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/8/fd71becd-9b03-4b91-8878-c0d14752079b/fd71becd-9b03-4b91-8878-c0d14752079b4.gif)
![ruby on rails 創(chuàng)建一個博客項目_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/8/fd71becd-9b03-4b91-8878-c0d14752079b/fd71becd-9b03-4b91-8878-c0d14752079b5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、控制臺創(chuàng)建一個博客項目rails new blogcd blograils g scaffold post title:string text:textrails g model comment post_id:integer text:textrake db:migraterails s測試:http:/localhost:3000/posts修改index.html.erbblog/app/views/posts/index.html.erb刪除原有的內(nèi)容,新的內(nèi)容如下:1. <h1><%= post.title %></h1>
2、60; 2. 3. <%= post.text %> 4. 5. <p> 6. <%= link_to "Back", posts_path %> 7. | 8. <%= link_to "Edit", edit_post_path(post) %>
3、160;9. | 10. <%= link_to "Delete",post,:method => :delete, :confirm => "Are you sure?" %> 11. </p> 來源: 增加Comments項繼續(xù)修改show.html.erbblog/app/views/posts/show.html.erb1. <h1>&
4、lt;%= post.title %></h1> 2. 3. <%= post.text %> 4. 5. <h2>Comments</h2> 6. 7. <% ments.each do |comment| %> 8. <p><%= com
5、ment.text %></p> 9. <p><%= time_ago_in_words comment.created_at %> ago </p> 10. <% end %> 11. 12. <%= form_for post,ments.build do |f| %>
6、160; 13. <p><%= f.text_area :text, :size => '40x10' %> </p> 14. <p><%= f.submit "Post Comment" %> </p> 15. <% end %>
7、 16. 17. <p> 18. <%= link_to "Back", posts_path %> 19. | 20. <%= link_to "Edit", edit_post_path(post) %> 21. | 22. <%= link_to &quo
8、t;Delete",post,:method => :delete, :confirm => "Are you sure?" %> 23. </p> 1. 修改blog/app/models/post.rb2. 增加 has_many :comments 修改blog/app/models/comment.rb增加 belongs_to :post
9、修改blog/config/routes.rb1. resources :posts do 2. resources :comments 3. end 這個時候Comment就出來了,但是如果提交comment,還會報錯,因為我們還沒寫comment的controller打開一個新的命令行D:Rubyprojects>cd blogD:Rubyprojectsblog>rails g controller comme
10、nts create destroy打開新創(chuàng)建的blogappcontrollerscomments_controller.rb1. class CommentsController < ApplicationController 2. def create 3. post = Post.find(params:post_id) 4. comment =
11、160;ments.build(comment_params) 5. comment.save 6. 7. redirect_to post 8. end 9. def comment_params params.require(:comment).permit(:id, :text)end10.
12、 def destroy 11. end 12. 13. end 14. 15. 增加刪除comment功能修改blog/app/views/posts/show.html.erb(增加Delete Comment鏈接)1. <h1><%= post.title %></h1> 2. 3. <%= post.text %> 4. &
13、#160; 5. <h2>Comments</h2> 6. 7. <% ments.each do |comment| %> 8. <p><%= comment.text %></p> 9. <p><%= time_ago_in_words comment.created_at
14、160;%> ago </p> 10. <p><%= link_to "Delete Comment", post, comment, :method => :delete, :confirm => "Are you sure?" %></p> 11. <%&
15、#160;end %> 12. 13. <%= form_for post,ments.build do |f| %> 14. <p><%= f.text_area :text, :size => '40x10' %> </p> 15. <p>
16、<%= f.submit "Post Comment" %> </p> 16. <% end %> 17. 18. <p> 19. <%= link_to "Back", posts_path %> 20. | 21. <%= link_t
17、o "Edit", edit_post_path(post) %> 22. | 23. <%= link_to "Delete",post,:method => :delete, :confirm => "Are you sure?" %> 24. </p> 修改blogappcontro
18、llerscomments_controller.rbclass CommentsController < ApplicationController def create post = Post.find(params:post_id) comment = ments.build(comment_params) comment.save redirect_to post end def destroy comment = Comment.find(params:id) comment.destroy redirect_to comment.po
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 供熱管道施工方案
- 牙周炎中免疫相關(guān)基因的生物信息學(xué)分析及免疫浸潤模式
- 2025公司合同違約賠償
- 非平衡自驅(qū)動體系與剪切體系內(nèi)在關(guān)聯(lián)的研究
- 二零二四年度新型環(huán)保墻地磚采購與銷售合作協(xié)議3篇
- 2025年度車場租賃與停車場信息化管理合同4篇
- 2025版國際勞務(wù)派遣公司服務(wù)協(xié)議范本3篇
- Unit 3 Seasons Lesson 1(說課稿)-2023-2024學(xué)年人教新起點版英語二年級下冊
- 二零二五版股份有限公司股權(quán)質(zhì)押貸款合同協(xié)議書3篇
- 2025公司之間借款合同范本
- 【采購管理優(yōu)化探究文獻(xiàn)綜述3000字】
- 《大學(xué)生職業(yè)發(fā)展與就業(yè)指導(dǎo)》課程標(biāo)準(zhǔn)
- 第23課《出師表》課件(共56張)
- GB/T 3953-2024電工圓銅線
- 發(fā)電機(jī)停電故障應(yīng)急預(yù)案
- 接電的施工方案
- 幼兒阿拉伯?dāng)?shù)字描紅(0-100)打印版
- 社會組織等級評估報告模板
- GB/T 12173-2008礦用一般型電氣設(shè)備
- 新媒體研究方法教學(xué)ppt課件(完整版)
- 2020新版?zhèn)€人征信報告模板
評論
0/150
提交評論