简体   繁体   中英

Rails 4 - if @model.save cases

How would I write this if/unless statement, so that it looks for IF this and UNLESS that?

respond_to do |format|
  if @request.save
    if @request.product.require_approval      ## UNLESS @request.project ##
      do stuff...
    else
      do other stuff...
    end
  else
        format.html { render action: 'new', notice: 'There was an error with your request.' }
        format.json { render json: @request.errors, status: :unprocessable_entity }
  end
end   

Thanks in advance.

Unfortunately I don't think you can combined the two. For your example I think the solution that is most clear would be stick with an if :

if @request.product.require_approval && !@request.project

There are some articles you can find online that specifically suggest avoiding chaining conditions together with unless , which is why I think you should stay with an if :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM