0

我正在使用 ROR 并且在我的控制器功能中我收到了这些参数的参数和基础,我需要根据条件执行操作。但我看到这些是大约 18 个条件。

我怎样才能干这个代码。

if params[:topic] == "Topic (title)" and params[:sort] == "Date (ASC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Date (DESC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Topic (ASC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Topic (DESC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Author (ASC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Author (DESC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Date (ASC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Date (DESC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Topic (ASC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Topic (DESC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Author (ASC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Author (DESC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Date (ASC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Date (DESC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Topic (ASC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Topic (DESC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Author (ASC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Author (DESC)"
  # custom code
end

非常感谢

4

3 回答 3

1

使用case... when语法。会让事情变得更清楚一些。还将所有 if 的第一部分移出案例 - 它们似乎在很多案例中很常见。

于 2013-02-20T14:07:50.133 回答
1

如果代码确实不同,请考虑使用 case 语句:

case [params[:topic], params[:sort]]
when ["Topic (title)", "Date (ASC)"]
  # custom code
when ["Topic (title)", "Date (DESC)"]
  # custom code
when ["Topic (title)", "Topic (ASC)"]
  # custom code
when ["Topic (title)", "Topic (DESC)"]
  # custom code
when ["Topic (title)", "Author (ASC)"]
  # custom code
when ["Topic (title)", "Author (DESC)"]
  # custom code
when ["Post (body)", "Date (ASC)"]
  # custom code
when ["Post (body)", "Date (DESC)"]
  # custom code
when ["Post (body)", "Topic (ASC)"]
  # custom code
when ["Post (body)", "Topic (DESC)"]
  # custom code
when ["Post (body)", "Author (ASC)"]
  # custom code
when ["Post (body)", "Author (DESC)"]
  # custom code
when ["Author", "Date (ASC)"]
  # custom code
when ["Author", "Date (DESC)"]
  # custom code
when ["Author", "Topic (ASC)"]
  # custom code
when ["Author", "Topic (DESC)"]
  # custom code
when ["Author", "Author (ASC)"]
  # custom code
when ["Author", "Author (DESC)"]
  # custom code
end

If there is repetition in the code, or you wind up using this case statement in multiple locations, then there are probably better ways to do this.

于 2013-02-20T15:40:36.387 回答
0


  topics = ['Topic (title)', 'Post (body)', 'Author' ]
  sorts = ['Date (ASC)', 'Date (DESC)', 'Topic (ASC)', 'Topic (DESC)']
  code_map = {
    [topics[0], sorts[0]] => ->() {
      # custom code
    },
    [topics[0], sorts[1]] => ->() {
      # custom code
    }
  }

  # usage
  code_map[[params[:topic], params[:sort]]()

虽然这消除了重复,但我不相信你想走这条路,因为我相信“自定义代码”也会有很多重复。

向我们展示您的“自定义代码”以及它在不同情况下的变化,我们可以为您干燥,而不是在调度级别

于 2013-02-20T14:23:03.993 回答