从视图调用外部API的Rails方法? [英] Rails way to call external API from view?
问题描述
我试图在我的一个视图中包含某种窗口小部件",以允许用户使用外部搜索" API.外部API返回JSON结果集.我想使用此JSON向用户显示结果列表.
I'm trying to include some sort of 'widget' in one of my views that allows the user to consume an external 'search' API. The external API returns a JSON set of results. I would like to use this JSON to show the user a list of results.
实现这一目标的途径"是什么?
What is the 'rails way' to make this happen?
例如,我想在主页上输入搜索输入,并带有一个按钮来搜索外部API.我已经创建了一个调用API的PORO,并且该PORO将返回结果,但是如何将用户的搜索字符串发送到该PORO?
For example, I would like to put a search input on the homepage, with a button to search the external API. I have created a PORO that calls the API and this PORO will return the results, but how do I take the users search string and post it to this PORO?
推荐答案
只需总结一下我们在评论中讨论的内容.完成所需操作的最简单方法是需要一个视图来呈现小部件,并需要一个route +控制器来处理搜索请求.
Just summing up what we discussed in the comments. The simplest way to accomplish what you want will need a view to render the widget and a route + controller to handle the search request.
这都是未经测试的代码,但是您可以了解它的要旨.
This is all untested code, but you can get the gist of it.
# routes.rb
# this can be named and pointed to anywhere you want.
# It's using get just so you can see the search params in the url, which is preety common for the search feature.
get '/search', to: 'searches#show', as: 'search'
# models/search.rb
# Your PORO
class Search
attr_reader :results
def initialize(query)
# perform request and assign results
@results = HTTParty.get(url, query: { keyword: query })
end
end
# controllers/searches_controller.rb
def show
if params[:query]
@search = Search.new(params[:query])
end
end
# views/searches/show.html.erb
<%= form_tag search_path, method: :get do %>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag %>
<% end %>
# .... renders contents if they're present.
<%= @search.results if @search %>
您还可以做的就是将Search变成模型,这样它将继承Naming,您可以将其作为常规资源来处理.
What you could additionally do is turn Search into a model, so it would inherit Naming and you could deal with it as a regular resource.
# routes.rb
# A singular resource will do.
resource :search, only: [:show]
# models/search.rb
include ActiveModel::Model
attr_reader :results
attr_accessor :query
validates :query, presence: true
def perform
@results = HTTParty.....
end
# controllers/searches_controller.rb
def show
if search_params = params[:search] && params[:search][:query]
@search = Search.new(query: search_params)
@search.perform if @search.valid?
else
@search = Search.new
end
end
# views/searches/show.html.erb
<%= form_for @search, method: :get do |f| %>
<%= f.text_field :query %>
<%= f.submit %>
<% end %>
<%= @search.results %>
现在...您可能想在所有位置而不是在/search中呈现窗口小部件,因此您可以将表单提取为部分表单,并在application.html.erb中进行呈现.
Now... You probably want to render your widget everywhere and not only in /search, so you can extract the form into a partial and render it in application.html.erb.
如果您使用ActiveModel方法,请不要忘记在呈现表单的每个请求中都初始化@search.您可以为此使用before_action
.
If you went for the ActiveModel approach don't forget to initialize @search in every request you render the form. You can use a before_action
for that.
这篇关于从视图调用外部API的Rails方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!