2010-06-11 35 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路由它,這是正確的。

相關問題