How do you use redirect_to when the action is depe

2019-07-30 19:09发布

问题:

How do you use redirect_to when the Edit action is dependent on a parameter being passed?

I have two very simple actions edit and update. Once I click submit, following an update I'd like to redirect the user back to the edit action. How do I do this if the edit action requires a parameter?

  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
  • this code blows up

回答1:

Try the following:

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

I assume here you declared your routes as resources :accounts