0

私は、ユーザーが経験と教育で履歴書を更新できるRoRアプリに取り組んでいる初心者です。これらの異なるアイテムのモデルが2つあり、時系列で一緒に表示したいと思います。プロファイルコントローラとビューでこれを設定する方法を知りたいです。

これと同じ方法を、ユーザーからの投稿アイテムとディスカッションアイテムを同じ方法で組み合わせるアプリの別の部分に採用したいと思います。しかし、現在、私の焦点は経験と教育の部分にあります。

プロファイル/show.html.erb:

    <% if @experiences.any? or @educations.any? %>
<div class="postExpOuter">
    <% @experiences.each do |experience| %>
            <div class="postExp">
                <div>
                    <h2><%= experience.position %></h2>
                    <h3><%= experience.company %> | <%= experience.location %></h3>
                    <h4>
                        <%= experience.start_month %> <%= experience.start_year %>
                        <% if experience.end_year %>
                            <%= " - " + experience.end_month %> <%= experience.end_year %>
                        <% else %>
                            <span> - Present</span>
                        <% end %>
                    </h4>
                </div>
            </div>
    <% end %>
    <% @educations.each do |education| %>
            <div class="postExp">
                <div>
                    <h2><%= education.degree %></h2>
                    <h3><%= education.school %></h3>
                    <h4>
                        <% if education.end_year %>
                            <span>Class of </span><%= education.end_year %>
                        <% else %>
                            <%= education.start_year %><span> - Present</span>
                        <% end %>
                    </h4>
                </div>
            </div>
    <% end %>
</div>
<% end %>
4

2 に答える 2

0

私のブラウザでハッキングされたばかりですが、それが直接機能するかどうかはわかりません。

<%- (@experiences.to_a + @educations.to_a).sort_by(&:end_year).each do |item| -%>
  <%- if item.is_a? Experience -%>

    your markup here...

  <%- else -%>

   your other markup here...

  <%- end -%>
<%- end -%>

それ以来ERBを書いていません...うーん、長いです。基本的には次のとおりです。2つのActiveRecord-Relationsを配列として1つの大きな配列にマージし、必要なタイムスタンプで並べ替えます(必要に.reverse応じて最後に追加できます)。リストを繰り返し処理しながら、オブジェクトのタイプを確認します。

お役に立てれば。

于 2013-01-15T16:19:20.370 に答える
-1

profiles_controller.rb:

class ProfilesController < ApplicationController
  def show
    @user = User.find_by_profile_name(params[:id])
    if @user
        @posts = @user.posts.all(:order => "created_at DESC", :limit => 3)
        @experiences = @user.experiences.all(:order => "start_year DESC")
        @educations = @user.educations.all(:order => "start_year DESC")

      @items = (@experiences.to_a + @educations.to_a).sort_by(&:start_year).reverse[0,3]

        render action: :show
    else
        render file: 'public/404', status: 404, formats: [:html] 
    end
  end
end

プロファイル/show.html.erb:

<% if @items.any? %>
<div class="postExpOuter">
    <% @items.each do |item| %>
      <% if item.is_a? Experience %>
       <div class="postExp">
            <div>
                <h2><%= item.position %></h2>
                <h3><%= item.company %> | <%= item.location %></h3>
                <h4>
                    <%= item.start_month %> <%= item.start_year %>
                    <% if item.end_year %>
                        <%= " - " + item.end_month %> <%= item.end_year %>
                    <% else %>
                        <span> - Present</span>
                    <% end %>
                </h4>
            </div>
        </div>
      <%- else -%>
        <div class="postExp">
            <div>
                <h2><%= item.degree %></h2>
                <h3><%= item.school %></h3>
                <h4>
                    <% if item.end_year %>
                        <span>Class of </span><%= item.end_year %>
                    <% else %>
                        <%= item.start_year %><span> - Present</span>
                    <% end %>
                </h4>
            </div>
        </div>
      <% end %>
    <% end %>
</div>
<% end %>
于 2013-01-15T17:05:32.403 に答える