Cat*_*ish 3 ruby ruby-on-rails ruby-on-rails-4
我正在尝试保存response并保存,issue如果它不在nil一个条件下,所以我没有多个if/else条件使这个逻辑复杂化.
对于@response存在且issue为零的用例,这不会进入if块.
有没有明显的东西我没有看到或者我不能在这样的一行中写出我的逻辑?
注意:我知道应该使用一个事务,但我现在只想尝试一个工作原型.
if @response.save && (issue.save unless issue.nil?) # Does not get into the if block when @response exists and issue is nil
p 'in save'
format.html { redirect_to issue_path(params[:issue_id]), notice: success_message }
else
p 'not in save'
format.html { render action: 'new' }
end
Run Code Online (Sandbox Code Playgroud)
这就是我现在所做的工作,我希望有一个更简单的1班轮而不是这个.
success = false
if issue.nil?
if @response.save
success = true
end
else
if @response.save && issue.save
success = true
end
end
if success
p 'in save'
format.html { redirect_to issue_path(params[:issue_id]), notice: success_message }
else
p 'not in save'
format.html { render action: 'new' }
end
Run Code Online (Sandbox Code Playgroud)
您希望在以下情况下执行"成功"条件:
@response.save 成功了issue是nil或issue不是nil,它成功保存.因此,你可以这样做;
if @response.save and (issue.nil? or issue.save)
# Success
else
# Fail
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
88 次 |
| 最近记录: |