如何将用户信息传递到每个语句轨道

时间:2019-10-22 13:22:54

标签: ruby-on-rails ruby httparty

所以我目前正在尝试将用户信息传递到rails中的each语句中,以便可以显示相册用户信息

这是我到目前为止所拥有的

index.html.erb

<main role="main" class="flex-shrink-0">
  <div class="container">
    <h1 class="mt-5">Albums</h1>
    <p class="lead">Here we show all albums and it's details</p>

    <div class="row">
      <% @albums.each do |album| %>
        <div class="col-md-4">
          <%# image_tag(@albumImage ['thumbnailUrl']) %>
          <h5 class="card-title">
            <%= album['title'] %>
          </h5>

          <p>By: <%= album.user['userId'] %><p>
          <%= link_to "View Album", album_path(album['id']), class: "btn btn-primary" %>
        </div>
      <% end %>
    </div>
  </div>

  <%= will_paginate @albums, renderer: WillPaginate::ActionView::BootstrapLinkRenderer, class: 'margin-auto' %>
</main>

album.rb

class Album < ApplicationRecord
  include HTTParty

  belongs_to :user
end

user.rb

class User < ApplicationRecord
  has_many :albums
end

album_controller.rb

require 'will_paginate/array'

class AlbumsController < ApplicationController
  def index
    @albums = HTTParty
      .get('https://jsonplaceholder.typicode.com/albums', :headers => {'Content-Type' => 'application/json'})
      .paginate(:page => params[:page], :per_page => 10)

    @user = HTTParty
      .get('https://jsonplaceholder.typicode.com/users/' + params[:id], :headers => {'Content-Type' => 'application/json'})
  end

  def show
    @album = HTTParty
      .get('https://jsonplaceholder.typicode.com/albums/' + params[:id], :headers => {'Content-Type' => 'application/json'})

    @albumPhotos = HTTParty
      .get('https://jsonplaceholder.typicode.com/photos?album=' + params[:id], :headers => {'Content-Type' => 'application/json'})
      .paginate(:page => params[:page], :per_page => 10)

    @user = HTTParty
      .get('https://jsonplaceholder.typicode.com/users/' + params[:id], :headers => {'Content-Type' => 'application/json'})
  end
end

我要从API获取所有信息,而没有数据库数据。显然,我正在尝试获取用户数据以将其放入each中,但我还想确保相册显示中的userID匹配项也能正确显示用户信息< / p>

1 个答案:

答案 0 :(得分:0)

您可以通过循环浏览将用户详细信息合并到相册,或者请同时更新用户和相册的API获取响应。

现在,

class AlbumsController < ApplicationController
   def index
      @albums = HTTParty.get('https://jsonplaceholder.typicode.com/albums', :headers => {'Content-Type' => 'application/json'}).paginate(:page => params[:page], :per_page => 10)

      @user = HTTParty.get('https://jsonplaceholder.typicode.com/users', :headers => {'Content-Type' => 'application/json'})

      @albums.each do |album|
        user = @users.first{|u| u if u.id == album.userId }
        album.merge!({name: user.name, email: user.email})  if user.present?
      end
  end
end

基于此,您可以根据您添加的条件(如果存在用户电子邮件,请检查您的情况)显示正确的用户信息,

<% @albums.each do |album| %>
    <% if album["email"].present? %>
        <div class="col-md-4">
          <%# image_tag(@albumImage ['thumbnailUrl']) %>
          <h5 class="card-title">
            <%= album['title'] %>
          </h5>

          <p>By: <%= album.user['userId'] %><p>
          <%= link_to "View Album", album_path(album['id']), class: "btn btn-primary" %>
        </div>
     <% end %>
 <% end %>