当我尝试搜索 API 时,使用 form_tag 我的实例变量没有保存
Using form_tag my instance variables are not saving when I try to search an API
我正在尝试创建一个简单的 Rails 应用程序来检索 API 数据。我的初衷不是将搜索保存到数据库中,这就是我使用 form_tag 的原因。当我在控制台中 运行 时,我的搜索有效,但是当我调用 @results
或 @first_match
时,它给我 nil。非常感谢。
Actor 控制器方法
def index
@results = API::Topic.search([:actor])
@first_match = @results.values.first
end
演员表
= form_tag 'actors/show', method: :get do
= text_field_tag "Actor"
= submit_tag "Show me"
路线
RailsApp::Application.routes.draw do
resources :actors
end
更新
由于我是在表单的show方法中路由的。我能够在控制器的 show 方法中检索变量。我不知道这是不是最好的方法。
def show
@results = API::Topic.search([:actor])
@first_match = @results.values.first
end
我想,你需要使用 API::Topic.search(params[:actor])
我正在尝试创建一个简单的 Rails 应用程序来检索 API 数据。我的初衷不是将搜索保存到数据库中,这就是我使用 form_tag 的原因。当我在控制台中 运行 时,我的搜索有效,但是当我调用 @results
或 @first_match
时,它给我 nil。非常感谢。
Actor 控制器方法
def index
@results = API::Topic.search([:actor])
@first_match = @results.values.first
end
演员表
= form_tag 'actors/show', method: :get do
= text_field_tag "Actor"
= submit_tag "Show me"
路线
RailsApp::Application.routes.draw do
resources :actors
end
更新
由于我是在表单的show方法中路由的。我能够在控制器的 show 方法中检索变量。我不知道这是不是最好的方法。
def show
@results = API::Topic.search([:actor])
@first_match = @results.values.first
end
我想,你需要使用 API::Topic.search(params[:actor])