Ruby on rails 有没有一种简单的方法可以用相同的参数调用不同的Ruby方法?

Ruby on rails 有没有一种简单的方法可以用相同的参数调用不同的Ruby方法?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有这样的代码 if star href = star_path( :"star[model]" => model.class, :"star[model_id]" => model.id )) else href = unstar_path( :"star[model]" => model.class, :"star[model_id]" => model.id )) end 如您所见,它调用star_路径或unstar_路径辅助对象,但参数相同。我觉得重复这

我有这样的代码

if star
  href = star_path( :"star[model]" => model.class, :"star[model_id]" => model.id ))
else
  href = unstar_path( :"star[model]" => model.class, :"star[model_id]" => model.id ))
end
如您所见,它调用star_路径或unstar_路径辅助对象,但参数相同。我觉得重复这样的参数很糟糕,感觉应该有更好的方法

谢谢

试试看

options = { :"star[model]" => model.class, :"star[model_id]" => model.id }

if star
  href = star_path(options)
else
  href = unstar_path(options)
end
两种方式:

  • 首先分配给变量

    path_options = :"star[model]" => model.class, :"star[model_id]" => model.id
    href = star ? star_path( path_options ) : unstar_path( path_options )
    
  • 使用自定义帮助器

    def custom_star_path( options = {} )
      action = options.delete( :action ) || :star
      action == :star ? star_path( options ) : unstar_path( options )
    end
    
    并致电:

    custom_star_path( :action => (:unstar unless star), :"star[model]" => model.class, :"star[model_id]" => model.id )
    
    或者更简单:

    def custom_star_path( options = {} )
      options.delete( :has_star ) ? star_path( options ) : unstar_path( options )
    end
    
    custom_star_path( :has_star => star, :"star[model]" => model.class, :"star[model_id]" => model.id )   
    

    • 如果您想使用变量方法,那么我认为
      send
      是一个不错的选择

      根据报告:

      调用由symbol/string标识的方法,并将指定的任何参数传递给它。如果名称send与obj中的现有方法冲突,则可以使用
      \uuuuu send\uuuu
      。当方法由字符串标识时,字符串将转换为符号


      尝试如下,简单的两行

      options = { :"star[model]" => model.class, :"star[model_id]" => model.id }
      
      href = star ? star_path(options) : unstar_path(options)
      

      切换星路径助手怎么样

      def toggle_star_path star, model
        options = { :"star[model]" => model.class, :"star[model_id]" => model.id }
        star ? unstar_path(options) : star_path(options)
      end
      
      那么在你看来,你只需调用:

      toggle_star_path star, model
      

      通过使用此处发布的其他解决方案,我决定:

      options = {:"star[model]" => model.class, :"star[model_id]" => model.id}
      href = send((star ? :unstar_path : :star_path ), options)
      

      你不能像href=star\u path(:“star[model]”=>model.class,:“star[model\u id]”=>model.id,:star\u unstar\u boolean\u flag=>True)这样做吗?我更喜欢将这些方法分开,因为它们指向不同的地方,做不同的事情。在这种情况下,使用三元运算符不是更好吗?像
      href=star?星型路径(选项):取消星型路径(选项)
      Send是最通用的解决方案。谢谢,我想知道,如果我们谈论的是一般用途,为什么超光速的答案没有被选中。他的答案更具可读性,因为这是我自己的答案,是从其他答案中提炼出来的。选择自己的感觉很粗鲁,我不同意。选择的答案应该是最好的,即使是你自己的答案。对我来说,这是所有答案中可读性最低的一个,这就是为什么我会选择你的答案而不是这个。当你只有一个选项哈希时,这可能是最好的解决方案。谢谢。这没有那么干,但我会随时挑选它,而不是里面有
      send
      的任何东西。
      toggle_star_path star, model
      
      options = {:"star[model]" => model.class, :"star[model_id]" => model.id}
      href = send((star ? :unstar_path : :star_path ), options)