Rails 使用特定动词重定向或渲染
我想在创建新项目时重定向到资源索引
这是控制器的一部分:
def create
@asset = Asset.new(params[:asset])
@assets = Asset.all
respond_to do |format|
if @asset.save
format.html { render :action => 'index' } ##########
format.xml { render :xml => @asset, :status => :created, :location => @asset }
else
format.html { render :action => "new" }
format.xml { render :xml => @asset.errors, :status => :unprocessable_entity }
end
end
end
我感兴趣的行标记为 ##########
我已经尝试过
format.html { redirect_to(assets_url) }
以及其他一些内容
它会重定向到正确的位置并很好地创建项目,问题是我无法让它不POST
。我需要将其获取到 GET
因为否则它会在我看来造成一些可怕的扭曲的事情。
I want to redirect to a resource index when a new item is created
Here is a piece of the controller:
def create
@asset = Asset.new(params[:asset])
@assets = Asset.all
respond_to do |format|
if @asset.save
format.html { render :action => 'index' } ##########
format.xml { render :xml => @asset, :status => :created, :location => @asset }
else
format.html { render :action => "new" }
format.xml { render :xml => @asset.errors, :status => :unprocessable_entity }
end
end
end
The line i'm interested is marked ##########
i've tried
format.html { redirect_to(assets_url) }
and some other stuff
It redirects to the right place and creates the item fine, the problem is that i cant get it to not POST
. I need to get it to GET
because otherwise it does some horribly screwy things to my view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
redirect_to :action =>; :index
或redirect_to assets_url
应该适合你。此外,index
操作始终是GET
请求。执行rake paths
来查看控制器中的每个操作发生了什么样的请求。redirect_to :action => :index
Orredirect_to assets_url
should work for you. Also,index
action is alwaysGET
request. Dorake routes
to see what kind of request happens for each action in your controller.