2010-06-11 97 views
19

我尝试通过传递控制器,动作和参数来重定向rails以显示动作。但是,rails完全忽略了动作的名称!Rails:redirect_to:controller =>'tips',:action =>'show',:id => @ tip.permalink

我得到的是 http://mysite/controllername/paramId

,所以我有错误讯息....

这里是我使用的动作代码:

def update 
    @tip = current_user.tips.find(params[:id]) 
    @tip.attributes = params[:tip] 
    @tip.category_ids = params[:categories] 
    @tip.tag_with(params[:tags]) if params[:tags] 


    if @tip.save 
     flash[:notice] = 'Tip was successfully updated.' 
     redirect_to :controller=>'tips', :action => 'show', :id => @tip.permalink 
    else 
     render :action => 'edit' 
    end 
    end 

回答

-3

这可能是一个临时的“避开”。使用渲染,它效果很好....

if @tip.save 
    flash[:notice] = 'Tip was successfully updated.' 
    render :action => 'show', :id => @tip.permalink 
# redirect_to :controller=>'tips', :action => 'show', :id => @tip.permalink 
else 
    render :action => 'edit' 
end 
35

为什么打架?

redirect_to @tip 

而且您可以使用:notice选项缩短代码。

redirect_to @tip, :notice => 'Message here' 
+0

我不知道有关:redirect_to上的通知快捷方式。谢谢,这肯定会节省一些时间。是否有相当于flash.now和render? – alternative 2010-06-12 11:06:31

+3

使用武力,卢克 – Rimian 2011-05-26 11:15:27

7

如果它是REST资源路由,则路由实际上是正确的。在/tips/id上的GET请求实际上调用show操作。由于它是路由的方式,我的猜测是,你用map.resources路由它,这是正确的。

相关问题