Ruby on rails Rails 3重定向
在rails中重定向是如何工作的?我觉得这很违反直觉,令人沮丧 我在Ruby on rails Rails 3重定向,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,在rails中重定向是如何工作的?我觉得这很违反直觉,令人沮丧 我在/views/mymodels/custom.html.erb 我有空的控制器方法: def custom end 我在routes.rb中有以下内容: resources :mymodel do member do get 'custom' end end 在我的控制器中,我尝试在以下人员创建模型时渲染自定义视图: respond_to do |format| if @presen
/views/mymodels/custom.html.erb
我有空的控制器方法:
def custom
end
我在routes.rb中有以下内容:
resources :mymodel do
member do
get 'custom'
end
end
在我的控制器中,我尝试在以下人员创建模型时渲染自定义视图:
respond_to do |format|
if @presentation.save
redirect_to action: "custom"
#format.html { redirect_to @mymodel, notice: 'Presentation was successfully created.'}
#format.json { render json: @mymodel, status: :created, location: @mymodel }
这会导致重定向呈现空白页。但是,浏览/mymodels/[id]/custom
可以正常工作。我做错了什么?为什么render:action=>“custom”
也不起作用
编辑:
这是有效的:
format.html{render action:“upload”}
但是为什么呢?你为什么评论format.html{…}
子句?使用它:
而不是:
if @presentation.save
redirect_to action: "custom"
#format.html { redirect_to @mymodel, notice: 'Presentation was successfully created.'}
写下:
if @presentation.save
format.html { redirect_to action: "custom" }
因为您将其放置在
respond_to
块中。注释此块并将您的重定向\u移到它之外并进行检查。所有这些都应该按照你的预期工作。但是你必须意识到,离开response\u to
只是为了好玩,而不是更多。在现实世界的应用程序中,您必须使用响应程序——它是一种可读的、RESTful的、符合MVC的方法,用于在各种if表示中显示单个资源。通过这种方式,您可以轻松地扩展产品,比如说产品
,以json/xml/其他格式表示,而无需添加新操作、更改路由和其他内容。