English 中文(简体)
修复——第11章,铁路3号
原标题:Refactoring - Chapter 11, Exercise 3 Rails Tutorial 2nd Edition
问题回答

您可以从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>

因为它基本上与共享的意见——用户_info.html.erb部分在主页上使用(Listing 10.32)相同。 因此,你可以把上述法典的编块改为:

<%= render  shared/user_info  %> 

Note that you will also need to add <% @user ||= current_user %> to the top of the viewsshared_user_info.html.erb partial (which is the same as what was necessary to add to the stats partial in Listing 11.20).

此外,在馈电-项目+与用户+微型插头部分的配料部分之间有一些重复(尽管没有准确的重复),根据该页的展示(在how、家或简介中),列出了一个或多个内容(姓名、 gr、行政删除链接、微型海报内容、微型张贴时间印章和微型删除链接)。 也许可以对这些内容进行重新评价,以便消除供料——项目+专用部分,并用视页数而定的用户+微调部分加以替换。

我刚刚通过这一努力,找到了一个行之有效的解决办法。

第一,对用户/用户/用户/用户_info.html.erb做了改动,以便在设定时使用“用户变量”和目前的用户变量。

app/views/shared/_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 %>

然后,我用部分用户_info.html.erb替换了 app/views/users/show_follow.hmtl.erb中的相应信息。

a. 用户/用户/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. Hartl的辅导。





相关问题
How would you refactor this bit of code?

This bit of code runs on Windows Compact Framework and what it does is obvious. It looks as it should be refactored (especially considering that I may want to add cmd.ExecuteResultSet() later), but I ...

How to avoid debugger-only variables?

I commonly place into variables values that are only used once after assignment. I do this to make debugging more convenient later, as I m able to hover the value on the one line where it s later ...

Java Null Conditional

I have the following pattern appearing many times in my Java code and was wondering how some of you might refactor it. Object obj1 = buildObj1(); if (obj1 != null) { return obj1; } Object obj2 = ...

Writing refactoring-friendly PHP code

As far as I know, and have gathered from other SO posts, there are no proper tools for refactoring PHP code yet, so when it comes to refactoring, it s probably good old search-and-replace for most of ...

Refactor packages in a Jar

I have a requirement that i need to load two versions of a jar at once. To avoid class path collisions I d like to rename the packages of one of the jars. Then in source you could always easily ...

热门标签