Search code examples
ruby-on-railsrubyruby-on-rails-5httparty

HTTParty cannot get id of album


I'm building a rails app and I'm trying to grab some data from an API.

This is my controller

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)
  end

  def show
  end
end

and this is my index view

<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">
          <h5 class="card-title">
            <%= album['title'] %>
          </h5>

          <p>User_id: <%= album['user_id'] %> | id: <%= album['id'] %><p>
          <%= link_to "View Album", album, class: "btn btn-primary" %>
        </div>
      <% end %>
    </div>
  </div>

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

The issue here is the link, I have resources :albums in the routes file.

                               albums GET    /albums(.:format)                                                                        albums#index
                                      POST   /albums(.:format)                                                                        albums#create
                            new_album GET    /albums/new(.:format)                                                                    albums#new
                           edit_album GET    /albums/:id/edit(.:format)                                                               albums#edit
                                album GET    /albums/:id(.:format)                                                                    albums#show
                                      PATCH  /albums/:id(.:format)                                                                    albums#update
                                      PUT    /albums/:id(.:format)                                                                    albums#update
                                      DELETE /albums/:id(.:format)                                                                    albums#destroy
                           users_show GET    /users/show(.:format)                                                                    users#show
                                 root GET    /                                                                                        albums#index

However, when I hover over the button it is linking to the following

image

How do I go about only linking to the ID i.e. localhost:3000/albums/2

UPDATE 1

routes.rb

Rails.application.routes.draw do
  resources :albums
  get 'users/show'

  root 'albums#index'
end

Solution

  • That should be easy.

    This is how you get the album id:

    album['id']
    

    And you can use the ID to get the link:

    <%= link_to "View Album", album_path(album['id']), class: "btn btn-primary" %>
    

    Warning: Path must be singular! album_path

    # add to your routes.rb
    resources :albums