0

我正在开发一个简单的应用程序,该应用程序要求我在更新操作后重定向到显示操作。

我有这条路线可供展示。

match '/user/:fullname' => 'items#show'

这给了我这个控制器。

@user = User.find_by_fullname(params[:fullname])

但是,在用户的更新方法之后,

def update
  if @user.update_attributes(params[:user])
    format.html { redirect_to @user } # Don't need to pass parameters here.
  else
    format.html { render action: 'edit' }
  end
end

它将我重定向到显示操作的原始格式,即:

sampleapp.dev/users/1

如何使用我在 routes.rb 中指定的全名格式重定向以显示操作?

sampleapp.dev/users/johndoe
4

1 回答 1

2

将此行更改为:

match '/user/:fullname' => 'items#show', :as => :custom_show_item

然后将您的重定向更改为:

redirect_to custom_show_item_path(:fullname => @user.fullname)
于 2012-09-17T05:28:23.867 回答