【问题标题】:How do you use redirect_to when the action is dependent on a parameter being passed?当操作依赖于传递的参数时,如何使用 redirect_to?
【发布时间】:2011-07-28 20:46:28
【问题描述】:

当 Edit 操作依赖于传递的参数时,如何使用 redirect_to?

我有两个非常简单的操作编辑和更新。单击提交后,在更新后,我想将用户重定向回编辑操作。如果编辑操作需要参数,我该怎么做?

  def edit
    @account = Account.find_by_user_id(@params['user_id'])
  end

  def update
    account = Account.find(params[:account_user_id])

       if account.update_attributes(params[:name])
        redirect_to params[:user_id].merge!(:action => :edit)
       else
         render :text => 'Sorry there was an error updating.'
       end
  end
  • 这段代码搞砸了

【问题讨论】:

    标签: ruby-on-rails ruby redirect parameters controller


    【解决方案1】:

    尝试以下方法:

     redirect_to edit_account_path( account.id, :user_id => params[:user_id])
    

    我假设您在这里将您的路线声明为resources :accounts

    【讨论】:

      猜你喜欢
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      • 2019-04-18
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      相关资源
      最近更新 更多