Rubocop->使用保护子句

时间:2019-05-17 00:38:55

标签: ruby-on-rails ruby-on-rails-5 rubocop

解决我的格式以避免rubocop错误的最佳方法是什么?还是有办法修改rubocop?哪个最好?

错误

  

Style / GuardClause:使用保护子句而不是包装代码   在条件表达式中。       如果@ saved_search.save

代码

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

1 个答案:

答案 0 :(得分:2)

如果只有这种行为(不考虑“ else”),则可以采用以下格式:

// more strict constraint
const func = <A extends B, B extends Pick<A, keyof A & keyof B>>() => {
    const aWithoutB: Omit<A, keyof B> = {} as any;
    const b: B = {} as any;    
    const a = {
        ...aWithoutB,
        ...b
    } as A; // I'm smarter than the compiler 
};

func<Alpha, Beta>(); // error now, as desired
func<Beta & { extraProp: string }, Beta>(); // okay

但是我建议您添加一个return unless @saved_search.save respond_to do |format| format.html { redirect_to saved_searches_path } format.js {} end 子句,以在保存返回else时注意。