浜松Rails3道場 其の四 View編

2,653 views
2,573 views

Published on

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

No Downloads
Views
Total views
2,653
On SlideShare
0
From Embeds
0
Number of Embeds
342
Actions
Shares
0
Downloads
17
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

浜松Rails3道場 其の四 View編

  1. 1. View Hamamatsurb#7 2011.09.14 @mackato11 9 14
  2. 2. 11 9 14
  3. 3. Wiki 5411 9 14
  4. 4. 11 9 14
  5. 5. 11 9 14
  6. 6. 11 9 14
  7. 7. 11 9 14
  8. 8. View - Ruby on Rails Guides Layouts and Rendering in Rails http://edgeguides.rubyonrails.org/layouts_and_rendering.html Rails Form helpers http://edgeguides.rubyonrails.org/form_helpers.html - Haml & Sass #haml http://haml-lang.com/ Sass - Syntactically Awesome Stylesheets http://sass-lang.com/11 9 14
  9. 9. Version 1.9.2 Version 3.0.9 Version 0.9.2 .rvmrc rvm ruby-1.9.2-p180@rails-3_0_911 9 14
  10. 10. GitHub git clone git://github.com/hamamatsu-rb/rails3dojo.git git checkout -b working 3-controller11 9 14
  11. 11. 11 9 14
  12. 12. 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 end11 9 14
  13. 13. 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
  14. 14. 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 <div class="error flash">Name cant be blank</div> end11 9 14
  15. 15. 11 9 14
  16. 16. 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
  17. 17. 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
  18. 18. 11 9 14
  19. 19. 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
  20. 20. Ruby library for GitHub Flavored Markdown ```ruby puts "Hello World!" ```` Rolling out the Redcarpet https://github.com/blog/832-rolling-out-the-redcarpet11 9 14
  21. 21. Gemfile gem "redcarpet" gem "albino" gem "nokogiri" Install Pygments(Python Syntax highlighter) % pip install pygments11 9 14
  22. 22. 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_safe end 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_s end #272 Markdown with Redcarpet - RailsCasts http://railscasts.com/episodes/272-markdown-with-redcarpet11 9 14
  23. 23. 11 9 14
  24. 24. 11 9 14
  25. 25. 11 9 14
  26. 26. app/views/pages/show.html.haml m(_ _)m11 9 14
  27. 27. 11 9 14
  28. 28. 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
  29. 29. 11 9 14
  30. 30. app/views/welcome/index.html.haml - content_for :title, "Welcome" %h1= yield :title %p Rails3dojo is the ... %br Its 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
  31. 31. git checkout master git merge working git branch -d working git clone git://github.com/hamamatsu-rb/rails3dojo.git git checkout 4-view11 9 14
  32. 32. 11 9 14
  33. 33. 11 9 14
  34. 34. 11 9 14
  35. 35. 11 9 14
  36. 36. 11 9 14
  37. 37. 11 9 14
  38. 38. 11 9 14

×