Mais conteúdo relacionado Semelhante a 浜松Rails3道場 其の四 View編 (20) Mais de Masakuni Kato (11) 浜松Rails3道場 其の四 View編1. View
Hamamatsurb#7 2011.09.14 @mackato
11 9 14
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. Version 1.9.2
Version 3.0.9
Version 0.9.2
.rvmrc
rvm ruby-1.9.2-p180@rails-3_0_9
11 9 14
10. GitHub
git clone git://github.com/hamamatsu-rb/rails3dojo.git
git checkout -b working 3-controller
11 9 14
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
end
11 9 14
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. 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 can't be blank</div>
end
11 9 14
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. 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
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. Ruby library for GitHub Flavored Markdown
```ruby
puts "Hello World!"
````
Rolling out the Redcarpet
https://github.com/blog/832-rolling-out-the-redcarpet
11 9 14
21. Gemfile
gem "redcarpet"
gem "albino"
gem "nokogiri"
Install Pygments(Python Syntax highlighter)
% pip install pygments
11 9 14
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-redcarpet
11 9 14
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
30. 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
31. git checkout master
git merge working
git branch -d working
git clone git://github.com/hamamatsu-rb/rails3dojo.git
git checkout 4-view
11 9 14