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

橙三吉。 提交于 2020-01-16 04:31:04

问题


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



来源:https://stackoverflow.com/questions/6865452/how-do-you-use-redirect-to-when-the-action-is-dependent-on-a-parameter-being-pas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!