简体   繁体   中英

Redirect_to another page in rails

In my application I have a set of entities. Now I want to build a search form on my start page that calls the action of controller a. If it finds more than one entitiy it shall show all the products if it finds exactly one product it should redirecto to another controller that loads the detailed information about the entity and shows it. In my first controller I do this by calling

if @entities.length==1
      redirect_to show_path(:id=>@entities[0].id)
    end

I would expect that now a new site is opened like /show?id=1234 but that does not happen. Instead the controller behind the entity path loads the detailed information of the entity but nothing is shown.

I get the following error:

ActionView::MissingTemplate (Missing template entities/show with {:formats=>[:js, :"*/*"], :handlers=>[:rjs, :rhtml, :rxml, :erb, :builder], :locale=>[:en, :en]} in view paths ..."):

How do I get the right page loaded, simply adding the show.js.erb to the entities folder makes the error disappear but the problem still remains that the show page is not shown.

EDIT:

 render :update do |page|
        page.redirect_to show_product_path(:id=>@entities[0].id)
      end

this works but why? what is the difference?

I would suggest to rederect straight to object. Rails is smart enough to create route for your object.

if @entities.length==1
  redirect_to @entities.first
end

I thnink

render :update do |page|
        page.redirect_to show_product_path(:id=>@entities[0].id)
end

code is looking for a show action in the same controller, where as

render :update do |page|
        page.redirect_to show_product_path(:id=>@entities[0].id)
 end

is redirecting to products/show in products controller. I think you dont have a 'show' action in 'entities' controller thats why you are getting

ActionView::MissingTemplate (Missing template entities/show with {:formats=>[:js, :"*/*"], :handlers=>[:rjs, :rhtml, :rxml, :erb, :builder], :locale=>[:en, :en]} in view paths ..."):

With the default rails configuration it works as follows

in your controller

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

So in you case you should redirect as

show_product_path with an id

and make sure you have show action defined in the controller

HTH

sameera

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM