Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 3 为什么Rails 3.2.2在使用redirect_to时生成前缀为/assets的URL?_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 3 为什么Rails 3.2.2在使用redirect_to时生成前缀为/assets的URL?

Ruby on rails 3 为什么Rails 3.2.2在使用redirect_to时生成前缀为/assets的URL?,ruby-on-rails-3,Ruby On Rails 3,标题问题大致概括了这一点,但我想详述一个场景 我已经创建了DemoController(我没有创建资源模型),我的routes.rb如下所示: DispatchMe::Application.routes.draw do root to: "demo#index" end 在演示控制器中,我将介绍以下内容: class DemoController < ApplicationController def index redirect_to :action => 's

标题问题大致概括了这一点,但我想详述一个场景

我已经创建了DemoController(我没有创建资源模型),我的routes.rb如下所示:

DispatchMe::Application.routes.draw do
  root to: "demo#index"
end
在演示控制器中,我将介绍以下内容:

class DemoController < ApplicationController
  def index
    redirect_to :action => 'show'
  end

  def show
  end
end
class DemoController“show”
结束
def秀
结束
结束
当然,app/views/demo/show.html.erb中有一个文件,我本以为会呈现该模板,但却发现以下错误:

ActionController::RoutingError(没有路由匹配[GET]“/assets”)

此URL是重定向的结果:

/assets?action=show&controller=demo

我是不是遗漏了什么?我认为rails应该为这种情况呈现动作的模板

注意。我知道,如果我创建一条路径,比如获取“显示”=>“演示显示”并调用重定向以显示路径,它将正常工作,但我需要知道这是否是必需的


多谢各位

对于所需的行为,请使用
render
而不是
redirect\u to

class PagesController < ApplicationController
  def index
    render :action => "show"
  end

  def show
  end
end
routes.rb

class DemoController < ApplicationController
  def index
  end

  def show
    redirect_to :action => 'index'
  end
end
DispatchMe::Application.routes.draw do
  root to: "demo#index"
  resources :demo
end
开发日志

Started GET "/demo/show" for 127.0.0.1 at 2012-04-04 14:55:25 -0400
Processing by DemoController#show as HTML
  Parameters: {"id"=>"show"}
Redirected to http://dispatch.dev/
Completed 302 Found in 0ms (ActiveRecord: 0.0ms)

是的,我自己也开始意识到这一点。然而,对我来说,rails不允许在没有路由定义的情况下使用重定向到,这几乎是不可接受的。render可以工作,但不会更新当前URL,这在本例中不是所需的行为。我希望对RoR有丰富经验的人能向我解释一下为什么现在这样工作。@user766388我已经更新了我的答案。如果你想要一个代码示例,请告诉我。很好,你的解释好多了。但既然你提出了,我就不能错过举个例子的机会。我真的很想看看。非常感谢。谢谢你的例子和提供的所有帮助,先生。