2013-10-14 20 views
0

有我的控制器許多檢查象下面這樣:如何在一行中正確重定向到@profile?

if @profile.expired 
    redirect_to profile_path(@profile.id) 
    return 
end 

是否有可能重構這一條線?

Also if using this code it redirects to /profiles.28 

,其中28是編號 如何將一個正確重定向到

/profiles/show/28 or profiles/28 ? 

回答

2

這很可能是你所需要的:

redirect_to(profile_path(@profile.id)) if @profile.expired 

你幾乎從來沒有(也幾乎從不,我的意思是從來沒有)在你的控制器代碼中需要return語句。如果你真的需要這種出於某種原因,你可以做

redirect_to(profile_path(@profile.id)) && return if @profile.expired 

至於您的其他問題,我猜你正確指定在你的路由的東西,如果你張貼他們,我很樂意更正。

0

我不是你想達到什麼完全清楚,但與我最好的猜測,你可以在做單行如下:

(@profile.expired) ? (redirect_to profile_path(@profile.id) and return) : 'do something else' 
相關問題