我有一个 OptionsController
,其中包含一个操作 account
。对应的view有3个form,分别post到3个不同的action,update_profile
、update_user
和change_password
。每个操作都会运行,然后应该重定向回 action
,在此处再次设置并呈现 View 。
我试图保持干爽并创建一个 after_filter
来进行重定向:
after_filter( :only => [:change_password, :update_profile, :update_user] ) do |controller|
controller.send(:redirect_to, :action => :account)
end
然而,这似乎并没有被调用;相反,该操作会提示无法找到其 View 。
Template is missing
Missing template options/update_user.erb in view path app/views
有什么方法可以让我以 DRY 方式执行此操作,还是我应该只在三个操作中的每个操作中坚持调用 redirect_to?
最佳答案
只需将 redirect_to
调用放入每个操作中。 DRY 和难以理解的魔法之间只有一线之隔。我觉得尝试做类似 after_filter
或任何其他不明显破坏预期行为的操作可能太神奇了。
关于ruby-on-rails - 如何在保持 DRY 的同时将多个操作重定向到单个操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1039000/