Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 重定向到搜索后显示页面-Rails_Ruby On Rails - Fatal编程技术网

Ruby on rails 重定向到搜索后显示页面-Rails

Ruby on rails 重定向到搜索后显示页面-Rails,ruby-on-rails,Ruby On Rails,在用户提交搜索后,我无法重定向到模特秀页面。搜索有两种方式。它搜索数据库以查看ETF是否存在。如果没有,它将抓取网页中的数据并将其保存在数据库中。搜索表单的路径如下:控制器: 类EtfsController

在用户提交搜索后,我无法重定向到模特秀页面。搜索有两种方式。它搜索数据库以查看ETF是否存在。如果没有,它将抓取网页中的数据并将其保存在数据库中。搜索表单的路径如下:控制器:

类EtfsController<应用程序控制器

  def search
    if params[:etf]
      @etf = Etf.find_by_ticker(params[:etf])
      @etf ||= Scraper.new_from_lookup(params[:etf])
    end
    if @etf.present?
      redirect_to @etf
    else
      puts "ETF was not found."
    end
  end

  def show
    @etf = Etf.find(params[:id])
    @top_holdings = @etf.top_holdings
    @country_weights = @etf.country_weights
    @sector_allocations = @etf.sector_allocations
  end

end
终端暗示它应该重定向到相应的ETF显示页面。然而,该网页不会转到新的相应URL

Started GET "/etfs/2" for ::1 at 2017-04-30 11:27:34 -0400
Processing by EtfsController#show as JS
  Parameters: {"id"=>"2"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Etf Load (0.1ms)  SELECT  "etfs".* FROM "etfs" WHERE "etfs"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]

  Rendering etfs/show.html.erb within layouts/application

  TopHolding Exists (0.1ms)  SELECT  1 AS one FROM "top_holdings" WHERE "top_holdings"."etf_id" = ? LIMIT ?  [["etf_id", 2], ["LIMIT", 1]]
  TopHolding Load (0.2ms)  SELECT "top_holdings".* FROM "top_holdings" WHERE "top_holdings"."etf_id" = ?  [["etf_id", 2]]
  SectorAllocation Exists (0.2ms)  SELECT  1 AS one FROM "sector_allocations" WHERE "sector_allocations"."etf_id" = ? LIMIT ?  [["etf_id", 2], ["LIMIT", 1]]
  SectorAllocation Load (0.2ms)  SELECT "sector_allocations".* FROM "sector_allocations" WHERE "sector_allocations"."etf_id" = ?  [["etf_id", 2]]
  CountryWeight Exists (0.2ms)  SELECT  1 AS one FROM "country_weights" WHERE "country_weights"."etf_id" = ? LIMIT ?  [["etf_id", 2], ["LIMIT", 1]]
  CountryWeight Load (0.3ms)  SELECT "country_weights".* FROM "country_weights" WHERE "country_weights"."etf_id" = ?  [["etf_id", 2]]

  Rendered etfs/show.html.erb within layouts/application (14.5ms)
Completed 200 OK in 148ms (Views: 130.6ms | ActiveRecord: 1.4ms)
以下是路线:

  root 'welcome#index'
  get 'search_etfs', to: 'etfs#search'
  get '/etfs/:id', to: 'etfs#show', as: 'etf'
理想情况下,我会使URL类似于显示搜索到的ETF的显示页面

为响应@Robikul添加更多信息:

我的搜索表单是通过ajax完成的:

<%= form_tag search_etfs_path, remote: true, method: :get, id: 'etf-lookup-form' do %>

从您的日志中,
由EtfsController处理#显示为JS
这似乎是一个
ajax请求
。请求的格式是
js
html
,这就是
重定向
无效的原因

如果成功,您可以创建一个
search.js.erb
文件,通过javascript使用
window.location=“etf/”
处理重定向。您的控制器必须具有可用的
@etf
变量

或者,您可以在控制器中的替换
重定向到@etf
中尝试以下代码段

render js: "window.location = '#{etf_path(@eft)}'"

谢谢rokibul。我将如何设置它以基于ETF进行动态渲染?即能够重定向到/etf/1、/etf/2、/etf/3等?我已经根据您的查询编辑了答案,请看一下。