ブログを作る
単純なブログを作ることを目標にしてみた
マイグレーションファイル
weblogsが記事、webleafsがコメントにします(postとcommentだと文字数が多いのでやめた)
rails generate migration CreateWeblogs
db/migrate/・・・create_weblogs.rb
class CreateWeblogs < ActiveRecord::Migration def change create_table :weblogs do |t| t.string :title t.string :category t.text :content t.integer :staff_id t.timestamps end create_table :webleafs do |t| t.integer :weblog_id t.text :content t.integer :staff_id t.timestamps end end end rake db:migrate
モデル
おまじない
app/models/weblog.rb
#encoding: utf-8 class Weblog < ActiveRecord::Base validates :title, :category, :content, :staff_id, :presence => true has_many :webleafs end
app/models/webleaf.rb
#encoding: utf-8 class Webleaf < ActiveRecord::Base validates :content, :staff_id, :presence => true belongs_to :weblog end
コントローラー
記事のコントローラー
app/controllers/weblog_controller.rb
#encoding: utf-8 class WeblogsController < ApplicationController before_filter :weblogs_sidemenu, :only=>[:edit, :new, :search, :show, :top] #新規登録 def create @weblog=Weblog.new(params[:weblog]) if @weblog.save flash[:notice]="ブログ記事を作成しました" redirect_to :action=>:show, :id=>@weblog.id else render :action=>:new end end #削除 def destroy Weblog.find(params[:id]).destroy flash[:notice]="ブログ記事を削除しました" redirect_to :action=>:top end #編集 def edit @weblog=Weblog.find(params[:id]) end #新規作成 def new @weblog=Weblog.new @weblog.staff_id=@current_user.member_id end #queryは単純な文字検索、categoryはカテゴリー専用の検索 def search @weblog_cond=find_weblog_search if request.get? if params[:page].nil? @weblog_cond.empty! @weblog_cond.query=params[:query] unless params[:query].nil? @weblog_cond.category=params[:category] unless params[:category].nil? end else @weblog_cond.set_params(params) redirect_to :action=>:search, :page=>1 end @weblogs = Weblog.paginate(:page=>params[:page], :per_page=>30, :conditions=>@weblog_cond.get_conditions, :include=>:webleafs, :order=>'weblogs.id DESC') end #表示、コメントを受け付ける def show @weblog=Weblog.find(params[:id]) @comments=Webleaf.where(:weblog_id => params[:id])#.order("id") @webleaf=Webleaf.new @webleaf.staff_id=@current_user.member_id #ログオンしていること @webleaf.weblog_id=params[:id] end #トップ def top @weblogs=Weblog.paginate(:page=>params[:page], :per_page=>5, :include=>:webleafs, :order=>'id DESC') end #更新 def update @weblog=Weblog.find(params[:id]) if @weblog.update_attributes(params[:weblog]) flash[:notice]="更新されました" redirect_to :action=>:show, :id=>@weblog else render :action=>:edit end end private # RailsによるアジャイルWebアプリケーションで絶賛していたセッションの扱い def find_weblog_search session[:weblog_cond] ||= Cond::WeblogCond.new end end
コメントのコントローラー
app/controllers/webleaf_controller.rb
#encoding: utf-8 class WebleafsController < ApplicationController before_filter :weblogs_sidemenu, :only=>:edit def create @webleaf=Webleaf.new(params[:webleaf]) if @webleaf.save flash[:notice]="コメントを投稿しました" redirect_to :controller=>:weblogs, :action=>:show, :id=>@webleaf.weblog_id else render :controller=>:weblogs, :action=>:show, :id=>@webleaf.weblog_id end end def destroy weblog_id=Webleaf.find(params[:id]).weblog_id Webleaf.find(params[:id]).destroy flash[:notice]="コメントを削除しました" redirect_to :controller=>:weblogs, :action=>:show, :id=>weblog_id end def edit @webleaf=Webleaf.find(params[:id]) end def update @webleaf=Webleaf.find(params[:id]) if @webleaf.update_attributes(params[:webleaf]) flash[:notice]="コメントを更新しました" redirect_to :controller=>:weblogs, :action=>:show, :id=>@webleaf.weblog_id else render :action=>:edit end end end
サイドメニューはapplication_controllerに設置する
app/controllers/application_controller.rb
#encoding: utf-8 class ApplicationController < ActionController::Base def weblogs_sidemenu @side_posts=Weblog.find(:all, :limit=>10, :order=>'id DESC') @side_comments=Webleaf.find(:all, :limit=>10, :order=>'id DESC') end end
最新の10件を表示する
検索用の仕掛け
app/models/cond/weblog_cond.rb
#encoding: utf-8 # ブログの検索 class Cond::WeblogCond < Cond::Cond attr_accessor :query,:category def empty! @query, @category="" end def get_conditions w=Where.new unless @query.blank? w.or("title LIKE ?", '%'+@query+'%') w.or{ |sw| sw.or("weblogs.content LIKE ?", '%'+@query+'%') sw.or("webleafs.content LIKE ?", '%'+@query+'%') } end w.and("category = ?", @category) unless @category.blank? w.to_s end def set_params(params) @query=params[:weblog_cond][:query] @category=params[:weblog_cond][:category] end end
ビューは?
コメントの編集だけ別になる。テンプレートファイルはweblogsから使う。
<div id="body_area"> <div id="blog"> <%= render :partial=>'weblogs/header' %> <div id="weblog"> <div id="comments"> <div id="webleaf"> <%= error_messages_for(:webleaf) -%> <%= form_for :webleaf, :url=>{:action=>:update, :id=>@webleaf}, :html=>{:name=>'frm'} do |f| -%> <h3>コメントを編集する</h3> <%= f.text_area(:content, :cols=>50, :rows=>10)%><br> <%= submit_tag '更新', :disable_with=>'更新中・・・', :class=>'button_link' %> <%= button_to '削除する', {:action=>:destroy, :id=>@webleaf }, :confirm=>'本当に削除していいですか?', :method=>:post, :class=>'button_link' if auth_check("destroy", @actions, @webleaf.staff_id) %> <%- end -%> </div> </div> <%== link_back %> </div> <%= render :partial=>'weblogs/side_menu' %> </div> </div>
だらだらなビューなので省略