Итак, я сейчас пытаюсь передать информацию о пользователях в оператор 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
в альбоме также показывают, чтобы он отображал правильную информацию о пользователе