2013-05-31 42 views
2

有一些「複雜的」路由,清除以下硬編碼鏈接並將它們命名爲路由會是一種好方法嗎?我在幾個地方有這些類型的構造,並希望讓它們脫離代碼。重構rails更新方法中硬編碼鏈接的最佳方式?

profiles_controller.rb

def update 

    @profile = Profile.find(params[:id]) 
    @tags = Session.tag_counts_on(:tags) 
    @profile.form = params[:form] 
    @match = Match.where(:user_id => current_user.id).first 
    authorize! :update, @profile 

    respond_to do |format| 
     if @profile.update_attributes(params[:profile]) 
     format.html { redirect_to "/me/#{ current_user.username }/edit/#{ @profile.form }", notice: t('notice.saved') } 
     else 
     format.html { render action: "/edit/edit_" + params[:profile][:form], :what => @profile.form } 
     end 
    end 
    end 

回答

2

您可以通過設置:as屬性創建自己的命名路由。

因此,對於成功更新重定向你必須有一個看起來像'後「/me/:user/edit/:form.format」的路線=>「型材#秀」

只要把, :as => :profiles在路線末尾,您可以改爲:profile_path(:user => current_user.username, :form => @profile.form)

在路由更改後運行rake routes會給出您可以使用哪些命名路由的列表。