使用 xhr 短路控制器操作 rails
Short Circuit a controller action with xhr for rails
在 Rails 中,有没有一种方法可以使一个动作与一个方法短路,并确保之后不会调用其他方法?
def update
return head(:unauthorized) unless available_user_settings?
if setting.update....
...
end
我想做更多类似的事情:
def update
ensure_settings_modifiable!
if setting.update....
...
end
但我不知道有什么好的方法可以渲染头部并在不应更新设置的情况下停止其余动作。
你可以:
def update
# stop action if `false` is returned. Otherwise continue
settings_modifiable? or return
...
end
您可以使用 before_action
类似的东西:
before_action :ensure_settings_modifiable!, only: [:update]
private
def ensure_settings_modifiable!
head(:unauthorized) unless available_user_settings?
end
因为如果 head
在 before_action
中调用,更新操作将不会执行
在 Rails 中,有没有一种方法可以使一个动作与一个方法短路,并确保之后不会调用其他方法?
def update
return head(:unauthorized) unless available_user_settings?
if setting.update....
...
end
我想做更多类似的事情:
def update
ensure_settings_modifiable!
if setting.update....
...
end
但我不知道有什么好的方法可以渲染头部并在不应更新设置的情况下停止其余动作。
你可以:
def update
# stop action if `false` is returned. Otherwise continue
settings_modifiable? or return
...
end
您可以使用 before_action
类似的东西:
before_action :ensure_settings_modifiable!, only: [:update]
private
def ensure_settings_modifiable!
head(:unauthorized) unless available_user_settings?
end
因为如果 head
在 before_action