使用的form_tag我的实例变量不保存,当我试图寻找一个API [英] Using form_tag my instance variables are not saving when I try to search an API

查看:148
本文介绍了使用的form_tag我的实例变量不保存,当我试图寻找一个API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个简单的Rails应用程序,将检索API数据。我的初衷不是保存搜索到数据库中,这就是为什么我用的form_tag。当我在控制台中运行我的搜索工作,但是当我打电话 @Results @first_match ,它给了我零。提前很多感谢。

演员控制器方法

 高清指数
    @Results = API :: Topic.search([:演员])
    @first_match = @ results.values​​.first
  结束

演员表

  =的form_tag'演员/秀,方法:做得到
  = text_field_tag​​演员
  = submit_tag让我看看

路线

  RailsApp :: Application.routes.draw做
   资源:演员
 结束


更新

由于我在表单中显示的方法路由。我能在我的节目在控制器方法来检索的变量。我不知道这是不是最好的办法,但。

 高清节目
    @Results = API :: Topic.search([:演员])
    @first_match = @ results.values​​.first
  结束


解决方案

我想,你需要使用 API :: Topic.search(PARAMS [:演员])

I'm trying to create a simple Rails app that will retrieve API data. My original intention were not to save the search to the database, which is why I used form_tag. My search works when I run in the console, but when I call @results or @first_match, it give me nil. Many thanks in advance.

Actor Controller Method

  def index
    @results = API::Topic.search([:actor])
    @first_match = @results.values.first
  end

Actor Form

= form_tag 'actors/show', method: :get do
  = text_field_tag "Actor"
  = submit_tag "Show me"

Routes

 RailsApp::Application.routes.draw do
   resources :actors
 end


Update

Since I am routing in the show method in the form. I was able to retrieve the variables in my show method in the controller. I don't know if this is the best way though.

  def show
    @results = API::Topic.search([:actor])
    @first_match = @results.values.first
  end

解决方案

I think, you need to use API::Topic.search(params[:actor])

这篇关于使用的form_tag我的实例变量不保存,当我试图寻找一个API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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