本文介绍了当操作依赖于传递的参数时,如何使用redirect_to?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当Edit操作依赖于传递的参数时,如何使用redirect_to?
我有两个非常简单的操作编辑和更新。一旦我点击提交,在更新后,我想将用户重定向到编辑操作。如果编辑操作需要参数,如何执行此操作?
def edit
@account = Account.find_by_user_id @params ['user_id'])
end
def update
account = Account.find(params [:account_user_id])
如果是帐户。 update_attributes(params [:name])
redirect_to params [:user_id] .merge!(:action =>:edit)
else
render:text => '对不起,更新时出错。'
end
end
- $ b
- this code blows up
redirect_to edit_account_path(account.id,:user_id => params [:user_id])
/ pre>
我假设你在这里将你的路由声明为
resources:accounts
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
解决方案
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
这篇关于当操作依赖于传递的参数时,如何使用redirect_to?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!