0

这些是我的控制器、视图和部分文件。我的问题是部分文件中的第二行。当项目等于@drafts 和等于@completed 我想写COMPLETED 时,我想在这里写草稿。我该如何解决?

控制器.rb

def projects
    @projects = @user.projects
    @drafts = @user.projects.draft
    @currents = @user.projects.current
    @completed = @user.projects.ended
    @user.projects.completed.each do |c|
      @completed << c
    end
    @all_projects = [@drafts,@currents,@completed]
  end

查看文件

<% for projects in @all_projects %>
  <%= render :partial => 'user_projects', :locals => {:projects => projects} %>
<% end %>

部分文件

<div class="panel">
        <h3>PROJECTS GENERAL NAME</h3>
        <div class="panel_contents">
          <% if projects.length == 0 %>
            <h2>There is no project.</h2>
          <% else %>
              <div class="attributes_table">
                <table cellspacing="0",cellpadding="0",border="0">
                  <thead>
                    <tr>
                      <th>Project Name</th>
                      <th>Created</th>
                      <th>Ended</th>
                      <th>Duration</th>
                      <th>Condition</th>
                      <th>Prize</th>
                    </tr>
                  </thead>
                  <tbody>
                     <% projects.each do |project| %>
                      <tr>
                        <td><%= link_to project.title, admin_projects_path('search[id_equals]' => project.id) %></td>
                        <td><%= l project.created_at, :format => :default  if project.created_at %></td>
                        <td><%= l project.end, :format => :default  if project.end %></td>
                        <td><%= Brief.find_by_project_id(project.id).duration  %></td>
                        <td><%= project.stage %></td>
                        <td><%= number_to_currency project.prize %></td>
                      </tr>
                    <% end %>
                  </tbody>
                </table>
              </div>
          <% end %>  
        </div>
      </div>
4

1 回答 1

0

最简单的方法是在部分中使用“projects.first”。假设字符串位于名为 status 的属性下

<h3><%= projects.first.status %></h3>

下面更复杂的解决方案

<% [[@drafts, 'Drafts'], [@completed, 'Completed']].each do |projects, title| %>
  <%= render 'user_projects', :projects => projects, :title => title %>
<% end %>

 # partial
<h3><%= title %></h3>
于 2013-02-10T02:03:24.750 回答