我从我的rails应用程序调用JSON api。用户在表单中输入名称,然后我调用JSON API并查找名称是否有任何匹配。有时它只是一个所以我用JSON中提供的数据创建条目。但是大多数时候JSON都有多个结果,我如何实现"嘿我找到了同名的多个结果,你找的是哪一个?"然后用户选择他/她想要的那个,然后在数据库中创建一个条目。
我写了一个简单的脚本来测试JSON。
data = JSON.load response
if data['results'].empty?
#if the JSON data is empty
puts "There was no movie found. Did you spell the movie name correctly?"
elsif data['results'].count > 1
#if JSON Data has more than one result
data['results'].each do |movie|
Movie::Movies << Movie.new(movie['id'],
movie['title'],
movie['release_year'],
(imdb_url + movie['imdb']).to_s,
movie['rating'],
movie['poster_120x171'],
movie['poster_240x342'],
movie['poster_400x570'])
end
Movie::Movies.each do |movie|
puts "#{movie.title} : #{movie.release_year}"
end
else
#IF JSON has only one entry
movie = Movie.new(data['results'][0]['id'],
data['results'][0]['title'],
data['results'][0]['release_year'],
(imdb_url + data['results'][0]['imdb']).to_s,
data['results'][0]['rating'],
data['results'][0]['poster_120x171'],
data['results'][0]['poster_240x342'],
data['results'][0]['poster_400x570'])
end
现在这是我的控制器。它只能处理JSON提供的Hash中的第一条记录。如果有多个结果,我不知道如何处理这种情况。
def create
require 'rest_client'
require 'json'
imdb_url = 'http://www.imdb.com/title/'
movie_title = movie_params['title'].delete(' ')
response = RestClient.get "http://api-public.guidebox.com/v1.43/US/xxxxxxxxxxxxxxxx<api-key>/search/movie/title/#{movie_title}"
data = JSON.load response
@movie = Movie.new #i am doing this because my for was giving error.
if data['results'].empty?
flash[:alert] = "Did you spell the Title correctly? Or Maybe we could not find the movie you are looking for."
render 'new'
elsif data['results'].count > 2
#Display all results to the user for them to pick what they really intended and create an entry of whaterver they choose.
else
@movie = Movie.new(gb_id: data['results'][0]['id'],
title: data['results'][0]['title'],
release_year: data['results'][0]['release_year'],
imdb_link: (imdb_url + data['results'][0]['imdb']).to_s,
rating: data['results'][0]['rating'],
small_img: data['results'][0]['poster_120x171'],
med_img: data['results'][0]['poster_240x342'],
large_img: data['results'][0]['poster_400x570'])
if @movie.save
flash[:notice] = 'Movie has been successfully Added.'
redirect_to @movie
else
flash[:alert] = "Something went wrong. Please try again."
render 'new'
end
end
end
同样在我的创建操作中,我不得不再次调用@movie = Movie.new
因为表单给了我错误。
答案 0 :(得分:0)
您很接近 - 允许用户消除结果的歧义,执行类似于您在else子句中的操作,但是在循环中,使用单独的操作。
# controller
def create
#...
if data['results'].empty?
flash[:alert] = "Did you spell the Title correctly? Or Maybe we could not find the movie you are looking for."
render 'new'
elsif data['results'].count > 1
@movie_choices = []
data['results'].each_with_index do |result, idx|
@movie_choices << Movie.new(gb_id: data['results'][idx]['id'],
title: data['results'][idx]['title'] #, ...
end
render 'movies/choose_movie'
else
@movie = Movie.new(gb_id: data['results'][0]['id'],
title: data['results'][0]['title'],
release_year: data['results'][0]['release_year'],
imdb_link: (imdb_url + data['results'][0]['imdb']).to_s,
rating: data['results'][0]['rating'],
small_img: data['results'][0]['poster_120x171'],
med_img: data['results'][0]['poster_240x342'],
large_img: data['results'][0]['poster_400x570'])
if @movie.save
flash[:notice] = 'Movie has been successfully Added.'
redirect_to @movie
else
flash[:alert] = "Something went wrong. Please try again."
render 'new'
end
end
end
def movie_chosen
chosen_movie_gb_id = params[:movie_gb_id]
@movie = Movie.new(gb_id: params[:movie_id],
title: params[:movie_title],
# ...
)
if @movie.save
flash[:notice] = 'Movie has been successfully Added.'
redirect_to @movie
else
flash[:alert] = "Something went wrong. Please try again."
render 'new'
end
end
# app/views/movies/choose_movie.html.erb
<%= form_tag 'movies/movie_chosen' do %>
<div>
<% @movie_choices.each do |mc| %>
<div>
<%= radio_button_tag :movie_id, mc.gb_id %> <%= mc.title %>
<%= hidden_field_tag :movie_title, mc.title %>
<!-- ... other fields of the movie object -->
</div>
<% end %>
</div>
<% end %>