[英]Redirect_to another page in rails
在我的應用程序中,我有一組實體。 現在,我想在起始頁面上構建一個搜索表單,該表單調用控制器a的動作。 如果找到多個實體,則應顯示所有產品;如果找到恰好一個產品,則應將其重定向到另一個控制器,該控制器加載有關實體的詳細信息並顯示出來。 在我的第一個控制器中,我通過調用
if @entities.length==1
redirect_to show_path(:id=>@entities[0].id)
end
我希望現在可以像/ show?id = 1234這樣打開一個新站點,但是不會發生。 相反,實體路徑后面的控制器加載了實體的詳細信息,但未顯示任何內容。
我收到以下錯誤:
ActionView::MissingTemplate (Missing template entities/show with {:formats=>[:js, :"*/*"], :handlers=>[:rjs, :rhtml, :rxml, :erb, :builder], :locale=>[:en, :en]} in view paths ..."):
我如何加載正確的頁面,只需將show.js.erb添加到entities文件夾中,該錯誤就會消失,但仍然存在顯示頁面未顯示的問題。
編輯:
render :update do |page|
page.redirect_to show_product_path(:id=>@entities[0].id)
end
這行得通,但是為什么呢? 有什么區別?
我建議直接改寫對象。 Rails足夠聰明,可以為您的對象創建路線。
if @entities.length==1
redirect_to @entities.first
end
我在想
render :update do |page|
page.redirect_to show_product_path(:id=>@entities[0].id)
end
代碼正在同一控制器中尋找表演動作,其中
render :update do |page|
page.redirect_to show_product_path(:id=>@entities[0].id)
end
正在重定向到產品/在產品控制器中顯示。 我認為您在“實體”控制器中沒有執行“顯示”操作,這就是為什么
ActionView::MissingTemplate (Missing template entities/show with {:formats=>[:js, :"*/*"], :handlers=>[:rjs, :rhtml, :rxml, :erb, :builder], :locale=>[:en, :en]} in view paths ..."):
使用默認的rails配置,其工作方式如下
在您的控制器中
class EntitiesController < ApplicationController
def index
#will display all the products
**#you need to have a index.erb.html file as well**
@products = <Your product getting logic here>
end
def show
#display only one product
#you need to have a show.erb.html
@product = Product.find(params[:id])
end
end
因此,在這種情況下,您應該重定向為
具有ID的show_product_path
並確保您已在控制器中定義了show action
高溫超導
薩梅拉
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.