任何人通過Chapter 11 Exercises for Michael Hartl's Rails Tutorial 2nd Edition工作?重構 - 第11章,練習3 Rails的教程第2版
第11章練習3問: 重構列表11.31通過爲以下/關注者頁面,主頁面和用戶顯示頁面添加通用代碼的部分內容。
,我沒有看到任何值得重構的網頁,用戶展示頁面,或show_follow頁面
如果有人想出了一些有意義的事情爲這項工作,很想知道。
謝謝!
任何人通過Chapter 11 Exercises for Michael Hartl's Rails Tutorial 2nd Edition工作?重構 - 第11章,練習3 Rails的教程第2版
第11章練習3問: 重構列表11.31通過爲以下/關注者頁面,主頁面和用戶顯示頁面添加通用代碼的部分內容。
,我沒有看到任何值得重構的網頁,用戶展示頁面,或show_follow頁面
如果有人想出了一些有意義的事情爲這項工作,很想知道。
謝謝!
您可以從清單11.31重構的一個代碼塊:
<section>
<%= gravatar_for @user %>
<h1><%= @user.name %></h1>
<span><%= link_to "view my profile", @user %></span>
<span><b>Microposts:</b> <%= @user.microposts.count %></span>
</section>
,因爲它本質上是相同的看法\ shared_user_info.html.erb部分的主頁上使用的(清單10.32)。因此,你可以替換上面這個代碼塊:
<%= render 'shared/user_info' %>
請注意,您還需要添加<% @user ||= current_user %>
到的意見頂部\ shared_user_info.html.erb部分(這是一樣的是什麼在清單11.20部分需要添加到統計數據中)。
此外,feed_item + feed部分與用戶+ micropost部分之間有一些重複(儘管不完全重複),其中取決於顯示哪個頁面(follow_show,home或profile),有一個或多個列出的元素(名稱,gravatar,管理員刪除鏈接,微博內容,微博時間戳和微博刪除鏈接)。這些可能也會被重構,以消除feed_item + feed部分,並根據頁面將它們替換爲用戶+微博部分的組合。
我只是通過這個練習的工作,發現一個可行的解決方案。
首先我周圍應用/視圖/共享/ _user_info.html.erb改變,如果它被設置爲使用@user變量,CURRENT_USER可變否則。
應用/視圖/共享/ _user_info.html.erb:
<% if @user %>
<%= link_to gravatar_for(@user, size: 52), @user %>
<h1>
<%= @user.name %>
</h1>
<span>
<%= link_to "view my profile", @user %>
</span>
<span>
<%= pluralize(@user.microposts.count, "micropost") %>
</span>
<% else %>
<%= link_to gravatar_for(current_user, size: 52), current_user %>
<h1>
<%= current_user.name %>
</h1>
<span>
<%= link_to "view my profile", current_user %>
</span>
<span>
<%= pluralize(current_user.microposts.count, "micropost") %>
</span>
<% end %>
然後我替換應用程序/視圖/用戶/ show_follow.hmtl.erb相應的信息與部分_user_info.html.erb
應用程序/視圖/用戶/ show_follow.hmtl.erb:
<div class="row">
<aside class="span4">
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<%= render 'shared/stats' %>
<% if @users.any? %>
<div class="user_avatars">
<% @users.each do |user| %>
<%= link_to gravatar_for(user, size: 30), user %>
<% end %>
</div>
<% end %>
</section>
</aside>
<div class="span8">
<h3><%= @title %></h3>
<% if @users.any? %>
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate %>
<% end %>
</div>
</div>
我希望這個答案可以幫助任何人通過M.哈特爾的教程去。
我找不到公共代碼要麼在這些網頁重構。也許這個問題是從先前版本的教程中得到的,其中沒有那麼多的重構代碼(?)。如果你正在尋找方法,以使代碼更緊湊,我可以建議使用Haml的重新寫你的觀點,也許走的是靜態內容從家庭,接觸,瞭解和幫助頁面,並且把它們放在降價文件你可以渲染。 – 2012-06-24 14:10:02