I’m developing a Ruby application that retrieves movie information. When the user hasn’t performed a search yet (when they first access the app), I want to implement fetching and displaying the popular horror movies. Currently, I can retrieve the movie information, but I’m unable to filter the data to get only the popular horror movies.
I can get the movie information, but I’m having trouble filtering the data to get only the popular horror movies. My issue is that I’m unable to filter the data to get only the popular horror movies.
app/views/movies/search.htmll.erb
<h2 class="text-white">Today's popular horror movies</h2>
<% movieinfo = JSON.parse((Tmdb::Movie.popular).to_json) %>
<% i = 0 %>
<div class="card-wrapper ">
<% while i < movieinfo['table']['results'].count %>
<div class="card">
<div class="card-upperinfo">
<%if movieinfo['table']['results'][i]['table']['title'].present?%>
<h3><%= link_to movieinfo['table']['results'][i]['table']['title'], movie_path(movieinfo['table']['results'][i]['table']['id'])%></h3>
<%end%>
<% if movieinfo['table']['results'][i]['table']['release_date'].present? %>
<p class="release-date"><%= movieinfo['table']['results'][i]['table']['release_date'] %></p>
<%end%>
</div>
<% if movieinfo['table']['results'][i]['table']['poster_path'].present? %>
<p><%= image_tag 'https://image.tmdb.org/t/p/w1280' + movieinfo['table']['results'][i]['table']['poster_path'],class: "card-img", size: "200x300" %></p>
<%end%>
</div>
<% i += 1%>
<%end%>
</div>
<%end%>
</div>
movies_controller.rb
class MoviesController < ApplicationController
require 'themoviedb-api'
Tmdb::Api.key(ENV['TMDB_API'])
Tmdb::Api.language("ja")
HORROR_GENRE_ID = 27
def search
if params[:looking_for]
movie_title = params[:looking_for]
url = "https://api.themoviedb.org/3/search/movie?api_key=#{ENV['TMDB_API']}&language=ja&query=" + URI.encode_www_form_component(movie_title)
else
url = "https://api.themoviedb.org/3/movie/popular?api_key=#{ENV['TMDB_API']}&language=ja"
text = params[:looking_for]
@movies = JSON.parse(Net::HTTP.get(URI.parse(url)))
end
end
def show
movie_id = params[:id]
url = "https://api.themoviedb.org/3/movie/#{movie_id}?api_key=#{ENV['TMDB_API']}&language=ja"
response = Net::HTTP.get(URI.parse(url))
@movie = JSON.parse(response)
@movie_history = MovieHistory.new
end
end
I referred to these articles.
How to fetch genre of a movie using its genre ids from tmdb api in android
How to get horror movies from TMDB?
yowayowa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.