用户提交搜索后,我无法重定向到模型显示页面。搜索有两种方式。它搜索数据库以查看ETF是否存在。如果没有,它会抓取数据的网页并将其保存在数据库中。搜索表单的路由是http://localhost:3000/search_etfs这是控制器:
类EtfsController< ApplicationController中
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显示页面。然而,该网页没有转到新的相应网址
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'
理想情况下,我会将网址设为http://localhost:3000/search_etfs?=SPY,以显示搜索到的ETF的展示位置。
添加更多信息以回应@Robikul:
我的搜索表单是通过ajax完成的:
<%= form_tag search_etfs_path, remote: true, method: :get, id: 'etf-lookup-form' do %>
答案 0 :(得分:1)
从您的日志Processing by EtfsController#show as JS
开始,它似乎是ajax request
。请求的格式为js
而非html
,这就是redirect
无效的原因。
如果成功,您可以创建一个search.js.erb
文件,通过javascript使用window.location = "etf/<%=@etf.id%>"
处理重定向。您的控制器必须具有@etf
变量。
或者您可以在控制器中替换redirect_to @etf
render js: "window.location = '#{etf_path(@eft)}'"