38
浜松Rails3道場 其の四 ViewHamamatsurb#7 2011.09.14 @mackato 11914日水曜日

浜松Rails3道場 其の四 View編

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 浜松Rails3道場 其の四 View編

浜松Rails3道場其の四 View編

Hamamatsurb#7 2011.09.14 @mackato

11年9月14日水曜日

Page 2: 浜松Rails3道場 其の四 View編

浜松Rails3道場の心得

習うより慣れろ

理論より実用

未知を恐れない

11年9月14日水曜日

Page 3: 浜松Rails3道場 其の四 View編

実際にRailsアプリを作ってみる

Wiki

全54回位で

11年9月14日水曜日

Page 4: 浜松Rails3道場 其の四 View編

Wikiの仕様

ページの編集と削除ができる

ページにコメントできる

ページの表示ができる

11年9月14日水曜日

Page 5: 浜松Rails3道場 其の四 View編

Wikiの仕様

ページの編集履歴が確認できる

ユーザー認証ができる

11年9月14日水曜日

Page 6: 浜松Rails3道場 其の四 View編

Wikiの仕様

ページの一覧が表示できる

11年9月14日水曜日

Page 7: 浜松Rails3道場 其の四 View編

Wikiの仕様ページが作成できる

11年9月14日水曜日

Page 9: 浜松Rails3道場 其の四 View編

開発環境

Ruby Version 1.9.2

Rails Version 3.0.9

.rvmrcrvm ruby-1.9.2-p180@rails-3_0_9

Rake Version 0.9.2

11年9月14日水曜日

Page 10: 浜松Rails3道場 其の四 View編

サンプルコードの取得GitHubから其の四前のコードを取得

git clone git://github.com/hamamatsu-rb/rails3dojo.git

git checkout -b working 3-controller

前回終了時のタグ作業ブランチ

11年9月14日水曜日

Page 11: 浜松Rails3道場 其の四 View編

ログイン・ログアウト

11年9月14日水曜日

Page 12: 浜松Rails3道場 其の四 View編

app/controllers/application_controller.rb

helper_method :current_user def current_user @current_user ||= User.find(session[:user_id])\ if session[:user_id] @current_user rescue ActiveRecord::RecordNotFound => e nil end

11年9月14日水曜日

Page 13: 浜松Rails3道場 其の四 View編

app/views/layouts/application.html.haml

- if current_user %span#user_name= current_user.name | = link_to "Logout", session_path(session[:user_id]), :method => :delete - else = form_tag sessions_path, :id => "login_form" do %label{:for => "user_name"} Name = text_field_tag :user_name = hidden_field_tag :before_path, request.path = submit_tag "Login"

11年9月14日水曜日

Page 14: 浜松Rails3道場 其の四 View編

app/views/layouts/application.html.haml

%body = flash_tag

app/halpers/application_helper.rb

def flash_tag unless flash.empty? flash.each do |k, v| return content_tag(:div, v, :class => [k, :flash]) end end end <div class="error flash">Name can't be blank</div>

11年9月14日水曜日

Page 15: 浜松Rails3道場 其の四 View編

ページの作成・編集

11年9月14日水曜日

Page 16: 浜松Rails3道場 其の四 View編

app/views/pages/new.html.haml- content_for :title, "New Page"%h1= yield :title%div = render(:partial => 'form', :locals => { :method => :post, :button => "Create" })

app/views/pages/edit.html.haml- content_for :title, "Edit Page"%h1= yield :title%div = render(:partial => 'form', :locals => { :method => :put, :button => "Update" })

11年9月14日水曜日

Page 17: 浜松Rails3道場 其の四 View編

app/views/pages/_form.html.haml

= form_for @page, :method => method do |f| = f.label :title = f.text_field :title, :class => "full-width" = f.label :body = f.text_area :body, :class => "full-width", :rows => 20 = f.submit button - if method == :put and not @page.title.blank? or = link_to "Back", wiki_page_path(@page.title)

11年9月14日水曜日

Page 18: 浜松Rails3道場 其の四 View編

ページの表示

11年9月14日水曜日

Page 19: 浜松Rails3道場 其の四 View編

app/views/pages/show.html.haml- content_for :title, @page.title%h1= yield :title

= markdown(@page.body)%hr{:class => "half-bottom"}%p = page_update_info_tag(@page) - if current_user %br = link_to "Edit", edit_page_path(@page) | = link_to "Delete", page_path(@page), :method => :delete, :confirm => "Are you sure?"

11年9月14日水曜日

Page 21: 浜松Rails3道場 其の四 View編

Gemfile

gem "redcarpet"gem "albino"gem "nokogiri"

% pip install pygments

Install Pygments(Python Syntax highlighter)

11年9月14日水曜日

Page 22: 浜松Rails3道場 其の四 View編

app/helpers/application_helper.rb

def markdown(text) text.gsub!(/\[\[(.+)\]\]/, '[\1](/\1)') options = [:hard_wrap, :filter_html, :autolink, :fenced_code, :gh_blockcode] syntax_highligher(Redcarpet.new(text, *options).to_html).html_safeend def syntax_highligher(html) doc = Nokogiri::HTML(html) doc.search("//pre[@lang]").each do |pre| pre.replace Albino.colorize(pre.text.rstrip, pre[:lang]) end doc.to_send

#272 Markdown with Redcarpet - RailsCastshttp://railscasts.com/episodes/272-markdown-with-redcarpet

11年9月14日水曜日

Page 23: 浜松Rails3道場 其の四 View編

11年9月14日水曜日

Page 24: 浜松Rails3道場 其の四 View編

インデントおかしい><

11年9月14日水曜日

Page 25: 浜松Rails3道場 其の四 View編

コメント・履歴

11年9月14日水曜日

Page 26: 浜松Rails3道場 其の四 View編

長いんで割愛しますapp/views/pages/show.html.haml の下の方を

読んでください m(_ _)m

11年9月14日水曜日

Page 27: 浜松Rails3道場 其の四 View編

ページ一覧

11年9月14日水曜日

Page 28: 浜松Rails3道場 其の四 View編

app/views/pages/index.html.haml

- content_for :title, "Pages"

%h1= yield :title

- @pages.each do |page| %h4= link_to page.title, wiki_page_path(page.title) %p= page_update_info_tag(page)

11年9月14日水曜日

Page 29: 浜松Rails3道場 其の四 View編

ホームページ

11年9月14日水曜日

Page 30: 浜松Rails3道場 其の四 View編

app/views/welcome/index.html.haml

- content_for :title, "Welcome"%h1= yield :title

%p Rails3dojo is the ... %br It's made in = link_to "Hamamatsu.rb", "http://hamamatsu-rb.github.com/"

%p At first, please create your = link_to "Home page", wiki_page_path("Home")

11年9月14日水曜日

Page 31: 浜松Rails3道場 其の四 View編

Wikiの開発は一旦ここまで

git checkout 4-view

作業内容をmasterにマージgit checkout mastergit merge working

作業後のコードはGitHubにありますgit clone git://github.com/hamamatsu-rb/rails3dojo.git

git branch -d working

11年9月14日水曜日

Page 32: 浜松Rails3道場 其の四 View編

One more thing.

11年9月14日水曜日

Page 33: 浜松Rails3道場 其の四 View編

既知のバグPageやCommentが消せません><

11年9月14日水曜日

Page 34: 浜松Rails3道場 其の四 View編

既知のバグPageやCommentが消せません><

ログイン状態で削除をOKしても

11年9月14日水曜日

Page 35: 浜松Rails3道場 其の四 View編

既知のバグPageやCommentが消せません><

ログイン状態で削除をOKしても

怒られます

11年9月14日水曜日

Page 36: 浜松Rails3道場 其の四 View編

既知のバグPageやCommentが消せません><

ログイン状態で削除をOKしても

怒られます

しかもセッション消滅

11年9月14日水曜日

Page 37: 浜松Rails3道場 其の四 View編

Q&A

11年9月14日水曜日

Page 38: 浜松Rails3道場 其の四 View編

4ヶ月のお付き合い

ありがとうございました。

次回からは単回ものにします。

最後に

11年9月14日水曜日