rails 3 redirect_to将params传递给命名路由

mat*_*ace 5 ruby

即使有很多关于如何使用像redirect_to这样的散列将params传递给重定向的建议,我也找不到有关如何执行此操作的详细信息

:action => 'something', :controller => 'something'
Run Code Online (Sandbox Code Playgroud)

在我的应用程序中,我在路线文件中有以下内容

match 'profile'   =>  'User#show'
Run Code Online (Sandbox Code Playgroud)

我的节目动作像这样松散

def show
 @user = User.find(params[:user])
  @title = @user.first_name
end
Run Code Online (Sandbox Code Playgroud)

重定向发生在这样的同一个用户控制器中

   def register
    @title = "Registration"
    @user = User.new(params[:user])

    if @user.save
      redirect_to  '/profile'
    end
  end
Run Code Online (Sandbox Code Playgroud)

现在的问题是在寄存器行动时,我redirect_to的我怎么沿PARAMS传递这样我就可以抓住从数据库或更好的用户还没有......我已经有一个用户变量,这样我怎么能沿使用者对象传递给节目行动?

-马修

Mat*_*att 7

如果您正在进行重定向,Rails实际上302 Moved会向浏览器发送带有URL 的响应,浏览器将向该URL发送另一个请求.所以你不能像在Ruby中那样"传递用户对象",你只能传递一些url编码的参数.

在这种情况下,您可能希望将路由定义更改为:

match 'profile/:id' => 'User#show'
Run Code Online (Sandbox Code Playgroud)

然后像这样重定向:

redirect_to "/profile/#{@user.id}"
Run Code Online (Sandbox Code Playgroud)