user2012677
user2012677

Reputation: 5735

Rubocop -> Use a guard clause

What is the best way to fix my formatting to avoid the rubocop error? Or is there a way to modify rubocop? Which is best?

Error

Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression. if @saved_search.save

Code

   if @saved_search.save
      respond_to do |format|
        format.html { redirect_to saved_searches_path }
        format.js {}
      end
    end

Upvotes: 0

Views: 823

Answers (1)

Douglas Lise
Douglas Lise

Reputation: 1486

If you have only this behaviour (without consider the "else") so you could do in this format:

return unless @saved_search.save

respond_to do |format|
  format.html { redirect_to saved_searches_path }
  format.js {}
end

But I suggest you to add an else clause to take care when the save returns false.

Upvotes: 2

Related Questions