2

有一些“复杂”的路由,什么是清理硬编码链接并使其命名为路由的好方法?我在几个位置都有这种结构,并希望将它们从代码中删除。

profile_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
4

1 回答 1

2

:as您可以通过设置属性来创建自己的命名路由。

因此,对于成功的更新重定向,您必须有一个类似于 `post '/me/:user/edit/:form.format' => 'profiles#show' 的路由

只需放在, :as => :profiles路线的末尾,您就可以重定向到:profile_path(:user => current_user.username, :form => @profile.form)

在路由更改后运行rake routes将为您提供可以使用的命名路由列表。

于 2013-05-31T21:53:57.043 回答