第一篇:ruby on rails 創(chuàng)建一個博客項目
控制臺創(chuàng)建一個博客項目
rails new blog
cd blog
rails g scaffold post title:string text:text
rails g model comment post_id:integer text:text
rake db:migrate
rails s
測試:http://localhost:3000/posts
修改index.html.erb
blog/app/views/posts/index.html.erb 刪除原有的內(nèi)容,新的內(nèi)容如下:
1.2.3.4.5.6.7.8.9.10.> 11.
來源: http://blog.csdn.net/pan_tian/article/details/8763627
<%= @post.title %>
<%= @post.text %>
<%= link_to “Back”, posts_path %> |
<%= link_to “Edit”, edit_post_path(@post)%> |
<%= link_to “Delete”,@post,:method => :delete, :confirm => “Are you sure?” %
增加Comments項 繼續(xù)修改show.html.erb blog/app/views/posts/show.html.erb 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.>
<%= @post.title %>
<%= @post.text %>
Comments
<% @post.comments.each do |comment| %>
<%= comment.text %>
<%= time_ago_in_words comment.created_at %> ago
<% end %><%= form_for [@post,@post.comments.build] do |f| %>
<%= f.text_area :text, :size => '40x10' %>
<%= f.submit “Post Comment” %>
<% end %><%= link_to “Back”, posts_path %> |
<%= link_to “Edit”, edit_post_path(@post)%> |
<%= link_to “Delete”,@post,:method => :delete, :confirm => “Are you sure?” %23.
1.2.修改blog/app/models/post.rb 增加
has_many :comments
修改blog/app/models/comment.rb 增加 belongs_to :post 修改blog/config/routes.rb
1.2.3.resources :posts do
resources :comments
end
這個時候Comment就出來了,但是如果提交comment,還會報錯,因為我們還沒寫comment的controller 打開一個新的命令行 D:Rubyprojects>cd blog
D:Rubyprojectsblog>rails g controller comments create destroy
打開新創(chuàng)建的blogappcontrollerscomments_controller.rb
1.class CommentsController < ApplicationController 2.3.4.5.6.7.8.9.def create
@post = Post.find(params[:post_id])@comment = @post.comments.build(comment_params)@comment.save
redirect_to @post end
def comment_params
params.require(:comment).permit(:id, :text)end
10.11.def destroy
end
12.13.end
14.15.增加刪除comment功能
修改blog/app/views/posts/show.html.erb(增加Delete Comment鏈接)1.2.3.4.5.
<%= @post.title %>
<%= @post.text %>
Comments
6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.ure?“ %> 24.<% @post.comments.each do |comment| %><%= comment.text %>
<%= time_ago_in_words comment.created_at %> ago
<%= link_to ”Delete Comment“, [@post, comment], :method => :delete, :confirm => ”Are you sure?“ %>
<% end %>
<%= form_for [@post,@post.comments.build] do |f| %>
<%= f.text_area :text, :size => '40x10' %>
<%= f.submit ”Post Comment“ %>
<% end %><%= link_to ”Back“, posts_path %> |
<%= link_to ”Edit“, edit_post_path(@post)%> |
<%= link_to ”Delete“,@post,:method => :delete, :confirm => ”Are you s
修改blogappcontrollerscomments_controller.rb class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(comment_params)
@comment.save
redirect_to @post
end
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to @comment.post
end end
def comment_params params.require(:comment).permit(:id, :text)end
給你的blog添加ATOM feed
修改blogappcontrollersposts_controller.rb def index @posts = Post.all
respond_to do |format| format.html # index.html.erb format.json { render json: @posts } format.atom
end end 修改blogappviewslayoutsapplication.html.erb header區(qū)域中添加下面的代碼,用于顯示Atom的圖標(biāo) <%= auto_discovery_link_tag :atom, “/posts.atom” %>
一個博客就基本完成了。
第二篇:play手把手教你創(chuàng)建一個博客項目-08.添加身份認證
08.添加身份認證
在上一節(jié)是,我們?yōu)閼?yīng)用程序添加了管理區(qū)域(administration area)功能,現(xiàn)在我們將在這些管理區(qū)域中插入一些身份認證功能。幸運的是,play已經(jīng)為這個功能準備了一個模塊,這個模塊叫Secure(安全)。
在程序里允許使用Secure模塊
在yabe/conf/application.conf文件里允許使用Secure模塊,并重啟程序: # Import the secure module module.secure=${play.path}/modules/secure 重啟后,play應(yīng)用在控制臺顯示模塊已經(jīng)成功啟動的相關(guān)信息。
Secure模塊帶有一系列默認的路由,需要在yabe/conf/routes里引入(或定義自己的路由也行): # Import Secure routes * / module:secure 保護admin(此處指需要身份認證的)控制器
安全模塊提供了一個controllers.Secure控制器,它聲明了所有可能用到的攔截器。當(dāng)然,我們可以以繼承這個控制器的方法獲得其攔截器,但是java只允許單繼承,這就導(dǎo)致了一些問題。
為了避免單繼承帶來的限制,我們可以用@With來注釋admin控制器,以告訴play去調(diào)用相應(yīng)的攔截器:
package controllers;
import play.*;import play.mvc.*;
@With(Secure.class)public class Posts extends CRUD { } 同樣用于Comments, Users和Tags控制器。
Now if you try to access any admin action, you should get a log-in page: 事實上,現(xiàn)在你就可以試著輸入任意username/password對看看,它其實并沒有對身份進行認證。
定制身份認證處理
應(yīng)用程序必須提供一個controllers.Secure.Security實例來定制身份認證處理。通過繼承這個類來創(chuàng)建我們自己版本的Secure類,我們可以指定如何對用戶身份進行認證。
創(chuàng)建yabe/app/controllers/Security.java文件,重寫authenticate()方法: package controllers;
import models.*;
public class Security extends Secure.Security {
static boolean authenticate(String username, String password){ return true;} } 既然我們已經(jīng)擁有了User對象,那么就非常容易實現(xiàn)這個方法: static boolean authenticate(String username, String password){ return User.connect(username, password)!= null;} 現(xiàn)在打開http://localhost:9000/logout進行登錄嘗試,用戶名和密碼在initial-data.yml文件里定義,比如bob@gmail.com/secret。
重構(gòu)管理區(qū)域(administration area)
我們之前已經(jīng)使用CRUD模塊來實現(xiàn)管理區(qū)域,但是這個管理區(qū)域仍然沒有集成到博客UI里,接下來我們將在一個新的管理區(qū)域上開始工作。這個新的管理區(qū)域允許每個作者訪問他自己的博客。而超級用戶則繼續(xù)使用完整功能的管理區(qū)域。
接下來,讓我們?yōu)楣芾聿糠謩?chuàng)建一個新Admin控制器: package controllers;
import play.*;import play.mvc.*;
import java.util.*;
import models.*;
@With(Secure.class)public class Admin extends Controller {
@Before static void setConnectedUser(){ if(Security.isConnected()){ User user = User.find(“byEmail”, Security.connected()).first();renderArgs.put(“user”, user.fullname);} }
public static void index(){ render();} } 重構(gòu)路由定義yabe/conf/routes: # Administration GET /admin/? Admin.index * /admin module:crud 請注意路由的順序,第一行就匹配了的http請求相應(yīng)的action會率先使用,同時忽略在其之下的路由配置。也就是說Admin控制器必須位于第二行之上,第二條路由將匹配所有其他的/admin請求,用于調(diào)用CRUD模塊頁面,否則/admin/將映射到CRUD.index而不是Admin.index。
現(xiàn)在把yabe/app/views/main.html模塊里的 ‘Log in to write something’文本修改到Admin.index控制器action:
…
…最后一件事就是為yabe/app/views/Admin/index.html模板文件完成所有的填充工作,讓我們從簡單的開始: Welcome ${user}!現(xiàn)在回到主頁,單擊 ‘Log in to write something’鏈接就回進入樣的管理區(qū)域頁面: 非常好!但是既然我們已經(jīng)有幾個管理區(qū)域的頁面,那么,我們就應(yīng)該有一個超級模板以重用代碼,讓我們創(chuàng)建一個yabe/app/views/admin.html模板:
#{get 'moreStyles' /}
第三篇:play手把手教你創(chuàng)建一個博客項目-03.構(gòu)建第一個頁面
03.構(gòu)建第一個頁面
之前,我們已經(jīng)編寫好了數(shù)據(jù)模型,是時候為應(yīng)用程序創(chuàng)建第一個頁面了。這個頁面只顯示當(dāng)前發(fā)表的博文完整內(nèi)容,同時顯示之前發(fā)表的博文列表。下面是該頁面結(jié)構(gòu)示意圖:
在啟動時加載默認數(shù)據(jù)
事實上,在編寫第一個頁面之前,我們還需要做一些工作。在一個沒有任何測試數(shù)據(jù)的頁面上進行工作并不太好,你甚至不能進行任何測試。
為博客程序注入測試數(shù)據(jù)的一條途徑就是在應(yīng)用程序啟動時加載一個固定文件。為了實現(xiàn)這個目的,我們將創(chuàng)建一個引導(dǎo)任務(wù)(Bootstrap Job)。一個play job 任務(wù)就是一在沒有任何http請求的情況下執(zhí)行一些特定工作,比如在應(yīng)用程序啟動時或指定時間間隔時使用CRON任務(wù)。
接下來,讓我們創(chuàng)建/yabe/app/Bootstrap.java job文件,使用Fixtures加載一系列默認數(shù)據(jù):
import play.*;import play.jobs.*;import play.test.*;
import models.*;
@OnApplicationStart public class Bootstrap extends Job {
public void doJob(){ // 檢查數(shù)據(jù)庫是否為空 if(User.count()== 0){ Fixtures.loadModels(“initial-data.yml”);} } } 在這里,我們使用@OnApplicationStart來注釋這個Job,用于告訴play我們打算在應(yīng)用程序啟動時同步運行這個任務(wù)。
事實上,在DEV和PROD模式下,這個任務(wù)的運行情況有所不同。在DEV模式下,play會等待第一個請求達到時才運行任務(wù)。因此這個任務(wù)會在第一個請求到達時才同步執(zhí)行,也就是說,如果這個任務(wù)失敗,你會在瀏覽器里看到錯誤消息。在PROD模式里,這個任務(wù)將會在應(yīng)用程序啟動時就執(zhí)行(與play run命令同步),以防止應(yīng)用程序在啟動時發(fā)生錯誤。
你必須在yabe/conf/下創(chuàng)建一個initial-data.yml文件。當(dāng)然,你也可以重用我們之前在data.yml里定義的內(nèi)容。
博客主頁
這次,我們將真正開始編寫主頁代碼。
還記得程序的第一個頁面是如何顯示的嗎?首先是在routes文件里指定/ URL 將調(diào)用controllers.Application.index()action方法,然后這個index()調(diào)用render()方法渲染/yabe/app/views/Application/index.html模板。我們將保留這些組件,但是我們會在其中添加代碼來加載博文列表并顯示。打開/yabe/app/controllers/Application.java控制器并修改index()action來加載博文列表:
package controllers;
import java.util.*;
import play.*;import play.mvc.*;
import models.*;
public class Application extends Controller {
public static void index(){ Post frontPost = Post.find(“order by postedAt desc”).first();List
olderPosts = Post.find(“order by postedAt desc”).from(1).fetch(10);render(frontPost, olderPosts);} } 看到我們是如何向render方法傳遞對象的嗎?通過這種方式,我們就可以在模板里使用相同的名稱來訪問這些對象了。在上面的代碼里,我們在模板里就可以直接使用變量frontPost和olderPosts。
打開/yabe/app/views/Application/index.html并修改,用于顯示這些對象: #{extends 'main.html' /} #{set title:'Home' /}
#{if frontPost}