Rails 从视图中调用外部 API 的方法? [英] Rails way to call external API from view?

查看:18
本文介绍了Rails 从视图中调用外部 API 的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的一个视图中包含某种小部件",以允许用户使用外部搜索"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?

推荐答案

只是总结我们在评论中讨论的内容.完成您想要的最简单的方法是需要一个视图来呈现小部件和一个路由 + 控制器来处理搜索请求.

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.

这篇关于Rails 从视图中调用外部 API 的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆