Controller              Hamamatsurb#5 2011.08.10 @mackato11   8   16
11   8   16
Wiki              511   8   16
11   8   16
11   8   16
11   8   16
11   8   16
Controller                                - Ruby on Rails Guides              Action Controller Overview              : ht...
Version 1.9.2                           Version 3.0.9        Up!                           Version 0.9.2        Up!       ...
GitHub         git clone git://github.com/hamamatsu-rb/rails3dojo.git         git checkout -b working 2-model11   8   16
Skinny Controller, Fat Model              1)HTTP               - params, request, session              2)         Model   ...
11   8   16
config/route.rb     resources :sessions     SessionsController      #create      #destroy11   8   16
spec/controllers/sessions_controller_spec.rb     describe "#create" do         context "ログインに成功した場合" do           before d...
spec/controllers/sessions_controller_spec.rb     describe "#create" do         context "ログインに失敗した場合" do           before d...
spec/controllers/sessions_controller_spec.rb     describe "#create" do         context "存在しないユーザー名でログイン要求した場合" do         ...
app/controllers/sessions_controller.rb         def create           user = User.find_or_create_by_name(params[:user_name]) ...
app/controllers/application_controller.rb     class ApplicationController < ActionController::Base       protect_from_forg...
spec/controllers/sessions_controller_spec.rb     describe "#destroy" do         before do           @user = create_user   ...
app/controllers/sessions_controller.rb         def destroy           session.delete(:user_id)           redirect_to params...
11   8   16
config/route.rb     resources :pages     match :title => pages#show, :via => [:get], :as => :wiki_page     PagesController ...
spec/controllers/pages_controller_spec.rb         describe "#index" do           before do             create_page(:user =...
app/controllers/pages_controller.rb         def index           @pages = Page.recent         end11   8   16
spec/controllers/pages_controller_spec.rb         describe "#show" do              it "GET wiki_page_path(title)にマッチ" do  ...
spec/controllers/pages_controller_spec.rb         describe "#show" do              context "Pageが存在する場合" do               ...
spec/controllers/pages_controller_spec.rb         describe "#show" do              context "Pageが存在しない場合" do              ...
app/controllers/pages_controller.rb         def show           @page = Page.find_by_title(params[:title])           unless ...
spec/controllers/pages_controller_spec.rb         describe "#new" do              context "ログインしていない場合" do                ...
app/controllers/application_controller.rb     class ApplicationController < ActionController::Base       protect_from_forg...
app/controllers/pages_controller.rb     before_filter :login_required, :except => [:index, :show]11   8   16
spec/spec_helper.rb     def login(user_name = nil)       user = user_name ? create_user(:name => user_name) : create_user ...
spec/controllers/pages_controller_spec.rb         describe "#new" do              context "ログイン済みの場合" do                be...
app/controllers/pages_controller.rb         def new           @page = Page.new         end11   8   16
spec/controllers/pages_controller_spec.rb         describe "#create" do              context "正しいデータが送信された場合" do          ...
spec/controllers/pages_controller_spec.rb         describe "#create" do              context "不正なデータが送信された場合" do          ...
app/controllers/pages_controller.rb         def create           @page = Page.new(params[:page])           @page.user = cu...
spec/controllers/pages_controller_spec.rb         describe "#edit" do              context "Pageが存在する場合" do               ...
spec/controllers/pages_controller_spec.rb         describe "#edit" do              context "Pageが存在しない場合" do              ...
app/controllers/pages_controller.rb         def edit           @page = Page.find(params[:id])         end11   8   16
spec/controllers/pages_controller_spec.rb         describe "#update" do           context "正しいデータが送信された場合" do             ...
spec/controllers/pages_controller_spec.rb         describe "#update" do              context "不正なデータが送信された場合" do          ...
app/controllers/pages_controller.rb         def update           @page = Page.find(params[:id])           @page.attributes ...
spec/controllers/pages_controller_spec.rb         describe "#destroy" do              context "Pageが存在する場合" do            ...
app/controllers/pages_controller.rb         def destroy           @page = Page.find(params[:id])           @page.destroy   ...
11   8   16
config/route.rb     resources :pages do       resources :comments     end     CommentsController      #create      #delete1...
spec/controllers/comments_controller_spec.rb         describe "#create" do              context "コメントが入力されている場合" do       ...
spec/controllers/comments_controller_spec.rb         describe "#create" do           context "コメントが入力されていない場合" do         ...
app/controllers/comments_controller.rb         before_filter :login_required         def create           @page = Page.find(...
spec/controllers/comments_controller_spec.rb         describe "#destroy" do              context "データを正しく指定した場合" do       ...
spec/controllers/comments_controller_spec.rb         describe "#destroy" do              context "データが存在しない場合" do         ...
app/controllers/comments_controller.rb         def destroy           @page = Page.find(params[:page_id])           @comment...
11   8   16
config/route.rb     root :to => "welcome#index"     WelcomeController      #index11   8   16
spec/controllers/welcome_controller_spec.rb         describe "#index" do              context "HomeというタイトルのPageが存在する場合" do...
spec/controllers/welcome_controller_spec.rb         describe "#index" do              context "Page Homeが存在しない場合" do      ...
app/controllers/welcome_controller.rb     class WelcomeController < ApplicationController       def index         @page = ...
git checkout master                      git merge working                      git branch -d working         git clone gi...
11   8   16
11   8   16
Upcoming SlideShare
Loading in...5
×

浜松Rails3道場 其の参 Controller編

3,557

Published on

2011/08/10のHamamatsu.rb#6で使った資料です。

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,557
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
22
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

浜松Rails3道場 其の参 Controller編

  1. 1. Controller Hamamatsurb#5 2011.08.10 @mackato11 8 16
  2. 2. 11 8 16
  3. 3. Wiki 511 8 16
  4. 4. 11 8 16
  5. 5. 11 8 16
  6. 6. 11 8 16
  7. 7. 11 8 16
  8. 8. Controller - Ruby on Rails Guides Action Controller Overview : http://edgeguides.rubyonrails.org/action_controller_overview.html11 8 16
  9. 9. Version 1.9.2 Version 3.0.9 Up! Version 0.9.2 Up! .rvmrc rvm ruby-1.9.2-p180@rails-3_0_911 8 16
  10. 10. GitHub git clone git://github.com/hamamatsu-rb/rails3dojo.git git checkout -b working 2-model11 8 16
  11. 11. Skinny Controller, Fat Model 1)HTTP - params, request, session 2) Model 3) - Model, flash 4) - render, redirect_to11 8 16
  12. 12. 11 8 16
  13. 13. config/route.rb resources :sessions SessionsController #create #destroy11 8 16
  14. 14. spec/controllers/sessions_controller_spec.rb describe "#create" do context "ログインに成功した場合" do before do @user = create_user post :create, :user_name => @user.name, :before_path => pages_path end it "ログイン前のページにリダイレクトする" do response.should redirect_to(pages_path) end it "セッションにログインユーザーのidがセットされる" do session[:user_id].should eql(@user.id) end it "ログインユーザーの情報が取得できる" do controller.current_user.should eql(@user) end end11 8 16
  15. 15. spec/controllers/sessions_controller_spec.rb describe "#create" do context "ログインに失敗した場合" do before do @create_user post :create, :user_name => "", :before_path => root_path end it "ログイン前のページにリダイレクトする" do response.should redirect_to(root_path) end it "エラーメッセージを表示する" do flash[:error].should be end end11 8 16
  16. 16. spec/controllers/sessions_controller_spec.rb describe "#create" do context "存在しないユーザー名でログイン要求した場合" do it "ログインは成功する" do post :create, :user_name => "tom", :before_path => root_path response.should redirect_to(root_path) end it "新しいユーザーが追加される" do lambda do post :create, :user_name => "tom", :before_path => root_path end.should change(User, :count).by(1) end end11 8 16
  17. 17. app/controllers/sessions_controller.rb def create user = User.find_or_create_by_name(params[:user_name]) if user.valid? session[:user_id] = user.id else flash[:error] = user.errors.full_messages.first end redirect_to params[:before_path] end11 8 16
  18. 18. app/controllers/application_controller.rb class ApplicationController < ActionController::Base protect_from_forgery def current_user @current_user ||= User.find(session[:user_id]) if session[:user_id] @current_user rescue ActiveRecord::RecordNotFound => e nil end end11 8 16
  19. 19. spec/controllers/sessions_controller_spec.rb describe "#destroy" do before do @user = create_user session[:user_id] = @user.id delete :destroy, :id => session[:user_id], :before_path => pages_path end it "ログアウト前のページにリダイレクトする" do response.should redirect_to(pages_path) end it "セッションのuser_idがクリアされる" do session[:user_id].should_not be end it "ログインユーザーの情報は取得できない" do controller.current_user.should_not be end11 8 16
  20. 20. app/controllers/sessions_controller.rb def destroy session.delete(:user_id) redirect_to params[:before_path] end11 8 16
  21. 21. 11 8 16
  22. 22. config/route.rb resources :pages match :title => pages#show, :via => [:get], :as => :wiki_page PagesController #index #show #new #create #edit #update #delete11 8 16
  23. 23. spec/controllers/pages_controller_spec.rb describe "#index" do before do create_page(:user => create_page.user) get :index end it "テンプレートindexを表示する" do response.should render_template(:index) end it "最新のPageのリストを表示する" do pages = assigns(:pages) pages.first.created_at.should > pages.last.created_at end end11 8 16
  24. 24. app/controllers/pages_controller.rb def index @pages = Page.recent end11 8 16
  25. 25. spec/controllers/pages_controller_spec.rb describe "#show" do it "GET wiki_page_path(title)にマッチ" do { :get => wiki_page_path(:title => "Home") }.should route_to("pages#show", :title => "Home") end11 8 16
  26. 26. spec/controllers/pages_controller_spec.rb describe "#show" do context "Pageが存在する場合" do before do @page = create_page get :show, :title => @page.title end it "テンプレートshowを表示する" do response.should render_template(:show) end it "タイトルが一致するPageを表示する" do assigns(:page).title.should eql(@page.title) end end11 8 16
  27. 27. spec/controllers/pages_controller_spec.rb describe "#show" do context "Pageが存在しない場合" do before do get :show, :title => "New Page" end it "テンプレートnewを表示する" do response.should render_template(:new) end it "タイトルは入力済み" do assigns(:page).title.should eql("New Page") end it "未作成Pageメッセージを表示する" do flash[:notice].should be end end11 8 16
  28. 28. app/controllers/pages_controller.rb def show @page = Page.find_by_title(params[:title]) unless @page flash.now[:notice] = "Page was not created yet." @page = Page.new(:title => params[:title]) render :action => :new end end11 8 16
  29. 29. spec/controllers/pages_controller_spec.rb describe "#new" do context "ログインしていない場合" do before do request.env[HTTP_REFERER] = pages_url get :new end it "元いたページにリダイレクトする" do response.should redirect_to(pages_url) end it "ログイン要求メッセージを表示する" do flash[:error].should be end end11 8 16
  30. 30. app/controllers/application_controller.rb class ApplicationController < ActionController::Base protect_from_forgery def current_user @current_user ||= User.find(session[:user_id]) if session[:user_id] @current_user rescue ActiveRecord::RecordNotFound => e nil end def login_required unless current_user flash[:error] = "This action is login required." url = request.env[HTTP_REFERER] || root_url redirect_to url end end end11 8 16
  31. 31. app/controllers/pages_controller.rb before_filter :login_required, :except => [:index, :show]11 8 16
  32. 32. spec/spec_helper.rb def login(user_name = nil) user = user_name ? create_user(:name => user_name) : create_user session[:user_id] = user.id end def logout controller.instance_variable_set("@current_user", nil) session.delete(:user_id) end def filtered_by_login_required url = request.env[HTTP_REFERER] || root_url response.should redirect_to(url) flash[:error].should be end11 8 16
  33. 33. spec/controllers/pages_controller_spec.rb describe "#new" do context "ログイン済みの場合" do before do login get :new end it "テンプレートnewを表示する" do response.should render_template(:new) end it "新規Page作成フォームを表示する" do assigns(:page).should be_new_record end end end11 8 16
  34. 34. app/controllers/pages_controller.rb def new @page = Page.new end11 8 16
  35. 35. spec/controllers/pages_controller_spec.rb describe "#create" do context "正しいデータが送信された場合" do before do @before_count = Page.count post :create, :page => { :title => "New Page" } end it "Wikiページにリダイレクトする" do response.should redirect_to(wiki_page_path("New Page")) end it "成功メッセージが表示される" do flash[:notice].should be end it "新しいPageが作成される" do Page.count.should eql(@before_count + 1) end end11 8 16
  36. 36. spec/controllers/pages_controller_spec.rb describe "#create" do context "不正なデータが送信された場合" do before do post :create, :page => { :title => "" } end it "テンプレートnewを表示する" do response.should render_template(:new) end it "入力エラーメッセージを表示する" do flash[:error].should be end end11 8 16
  37. 37. app/controllers/pages_controller.rb def create @page = Page.new(params[:page]) @page.user = current_user if @page.save flash[:notice] = "Page was created." redirect_to wiki_page_path(@page.title) else flash.now[:error] = "Page could not create." render :action => :new end end11 8 16
  38. 38. spec/controllers/pages_controller_spec.rb describe "#edit" do context "Pageが存在する場合" do before do @page = create_page(:user => controller.current_user) get :edit, :id => @page.id end it "テンプレートeditを表示する" do response.should render_template(:edit) end it "Page編集フォームを表示する" do assigns(:page).should_not be_new_record end end11 8 16
  39. 39. spec/controllers/pages_controller_spec.rb describe "#edit" do context "Pageが存在しない場合" do it "例外RecordNotFoundを投げる" do lambda do get :edit, :id => 99999 end.should raise_error(ActiveRecord::RecordNotFound) end end11 8 16
  40. 40. app/controllers/pages_controller.rb def edit @page = Page.find(params[:id]) end11 8 16
  41. 41. spec/controllers/pages_controller_spec.rb describe "#update" do context "正しいデータが送信された場合" do before do @page = create_page(:user => controller.current_user) @before_histries_count = @page.histories.count put :update, :id => @page.id, :page => { :title => "New Title" } end it "Wikiページにリダイレクトする" do response.should redirect_to(wiki_page_path("New Title")) end it "成功メッセージが表示される" do flash[:notice].should be end it "Pageが更新される" do assigns(:page).should be_valid assigns(:page).title.should eql("New Title") end it "Historyが追加される" do assigns(:page).histories.count.should eql(@before_histries_count + 1) end end11 8 16
  42. 42. spec/controllers/pages_controller_spec.rb describe "#update" do context "不正なデータが送信された場合" do before do @page = create_page(:user => controller.current_user) put :update, :id => @page.id, :page => { :title => "" } end it "テンプレートeditを表示する" do response.should render_template(:edit) end it "入力エラーメッセージを表示する" do flash[:error].should be end end11 8 16
  43. 43. app/controllers/pages_controller.rb def update @page = Page.find(params[:id]) @page.attributes = params[:page] if @page.save_by_user(current_user) flash[:notice] = "Page was updated." redirect_to wiki_page_path(@page.title) else flash.now[:error] = "Page could not update." render :action => :edit end end11 8 16
  44. 44. spec/controllers/pages_controller_spec.rb describe "#destroy" do context "Pageが存在する場合" do before do @page = create_page(:user => controller.current_user) @before_count = Page.count put :destroy, :id => @page.id end it "Homeにリダイレクトする" do response.should redirect_to(root_path) end it "成功メッセージを表示する" do flash[:notice].should be end it "Pageが削除される" do Page.count.should eql(@before_count - 1) end end11 8 16
  45. 45. app/controllers/pages_controller.rb def destroy @page = Page.find(params[:id]) @page.destroy flash[:notice] = "Page was deleted." redirect_to root_path end11 8 16
  46. 46. 11 8 16
  47. 47. config/route.rb resources :pages do resources :comments end CommentsController #create #delete11 8 16
  48. 48. spec/controllers/comments_controller_spec.rb describe "#create" do context "コメントが入力されている場合" do before do post :create, :page_id => @page.id, :comment => { :body => "Comment...." } end it "Wikiページにリダイレクトする" do response.should redirect_to(wiki_page_path(:title => @page.title)) end it "成功メッセージを表示する" do flash[:notice].should be end it "Commentが追加される" do assigns(:page).comments.count.should eql(@before_count + 1) end end11 8 16
  49. 49. spec/controllers/comments_controller_spec.rb describe "#create" do context "コメントが入力されていない場合" do before do post :create, :page_id => @page.id, :comment => { :body => "" } end it "Wikiページにリダイレクトする" do response.should redirect_to(wiki_page_path(:title => @page.title)) end it "エラーメッセージを表示する" do flash[:error].should be end end11 8 16
  50. 50. app/controllers/comments_controller.rb before_filter :login_required def create @page = Page.find(params[:page_id]) @comment = @page.comments.build(params[:comment]) @comment.user = current_user if @comment.save flash[:notice] = "Comment was created." else flash[:error] = "Comment could not create." end redirect_to wiki_page_path(:title => @page.title) end11 8 16
  51. 51. spec/controllers/comments_controller_spec.rb describe "#destroy" do context "データを正しく指定した場合" do before do delete :destroy, :page_id => @page.id, :id => @comment.id end it "Wikiページにリダイレクトする" do response.should redirect_to(wiki_page_path(:title => @page.title)) end it "成功メッセージを表示する" do flash[:notice].should be end it "コメントが削除される" do assigns(:page).comments.count.should eql(@before_count - 1) end end11 8 16
  52. 52. spec/controllers/comments_controller_spec.rb describe "#destroy" do context "データが存在しない場合" do it "例外RecordNotFoundを投げる" do lambda do delete :destroy, :page_id => 999, :id => @comment.id end.should raise_error(ActiveRecord::RecordNotFound) lambda do delete :destroy, :page_id => @page.id, :id => 999 end.should raise_error(ActiveRecord::RecordNotFound) end end11 8 16
  53. 53. app/controllers/comments_controller.rb def destroy @page = Page.find(params[:page_id]) @comment = @page.comments.find(params[:id]) @comment.destroy flash[:notice] = "Comment was deleted." redirect_to wiki_page_path(:title => @page.title) end11 8 16
  54. 54. 11 8 16
  55. 55. config/route.rb root :to => "welcome#index" WelcomeController #index11 8 16
  56. 56. spec/controllers/welcome_controller_spec.rb describe "#index" do context "HomeというタイトルのPageが存在する場合" do before do home = create_page(:title => "Home") create_page(:title => "Other", :user => home.user) get :index end it "テンプレート pages/show を表示する" do response.should render_template(pages/show) end it "Homeを表示する" do assigns(:page).title.should eql("Home") end end11 8 16
  57. 57. spec/controllers/welcome_controller_spec.rb describe "#index" do context "Page Homeが存在しない場合" do before do home = create_page(:title => "Other") get :index end it "テンプレート index を表示する" do response.should render_template(:index) end it "Pageはアサインされない" do assigns(:page).should_not be end end11 8 16
  58. 58. app/controllers/welcome_controller.rb class WelcomeController < ApplicationController def index @page = Page.find_by_title("Home") render :template => pages/show if @page end end11 8 16
  59. 59. git checkout master git merge working git branch -d working git clone git://github.com/hamamatsu-rb/rails3dojo.git git checkout 3-controller11 8 16
  60. 60. 11 8 16
  61. 61. 11 8 16
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×