简体   繁体   English

如何从Rails Controller中删除代码块

[英]How to remove code block from Rails Controller

I am trying to remove a code section from my controller so that it looks clean and thin. 我试图从我的控制器中删除一个代码段,以使其看起来干净整洁。 In one of my controller I have to use many conditions and that is making my controller look very heavy. 在我的一个控制器中,我必须使用许多条件,这使我的控制器看起来很沉重。 I just want to remove just that code block in other file and want to include it into my controller. 我只想删除其他文件中的该代码块,并希望将其包括在我的控制器中。 My controller code 我的控制器代码

Here you can see I am using some logic like: 在这里您可以看到我正在使用一些逻辑,例如:

   if search == 'Primary'
    if rain_fall_type == "Agriculture, Forestry and Fishing"

      if compare == "None"
        search = "None"
        rain_fall_type
      elsif compare == "All"
        rain_fall_type = compare
        data = [
          "Crops",
          "Livestock",
          "Forestry and Logging",
          "Fishing and Aquaculture",
        ]
      else
        search = "None"
        rain_fall_type = compare
      end

    elsif rain_fall_type == "None"
      rain_fall_type

      data = [
        "Primary",
      ]

    else
      data = [
        "Agriculture, Forestry and Fishing",
        "Mining and Quarrying",
      ]
    end
    ......

I want this section to be in some other files and want to call it as it is. 我希望此部分位于其他文件中,并按原样调用它。 I can write a function and a method and return values. 我可以编写一个函数和一个方法并返回值。 My question is can I call this code block as it is without returning function or anything. 我的问题是我可以按原样调用此代码块,而无需返回函数或任何东西。 For example I need to do something like this: 例如,我需要执行以下操作:

  a = if test == "a"
       #my code
    end

   #and in my controller 

  def test
     a 
  end

Can we do this in ruby put all code block in one file and call that value in my controller. 我们可以用ruby做到这一点吗?将所有代码块放在一个文件中,然后在我的控制器中调用该值。

To give you a short answer, so you can start right away you can do something like this: 为了给您一个简短的答案,您可以立即开始进行以下操作:

if search == 'Primary'
  data, search = RainFallSearcher.call(rain_fall_type, compare)
end

class RainFallSearcher
  def call(rain_fall_type, compare)
    if rain_fall_type == "Agriculture, Forestry and Fishing"
      if compare == "None"
        search = "None"
        rain_fall_type
      elsif compare == "All"
        rain_fall_type = compare
        data = [
          "Crops",
          "Livestock",
          "Forestry and Logging",
          "Fishing and Aquaculture",
        ]
      else
        search = "None"
        rain_fall_type = compare
      end
    elsif rain_fall_type == "None"
      data = [
        "Primary",
      ]
    else
      data = [
        "Agriculture, Forestry and Fishing",
        "Mining and Quarrying",
      ]
    end

    return data, search
  end
end

You can then have RainFallSearcher in a location that's loaded by Rails (I would choose app/services . 然后,您可以在Rails加载的位置安装RainFallSearcher (我会选择app/services

You are not forced to have all your logic in the controllers or the models, it's actually recommended to have them quite thin, and create simple (or complex) Ruby objects to deal with the logic. 您不必被迫将所有逻辑都包含在控制器或模型中,实际上建议您将它们简化,并创建简单(或复杂)的Ruby对象来处理逻辑。

One of the "patterns" I like in Rails is "Service Object" (there are various discussion and naming convention), so google that and you can get more information. 我喜欢在Rails中使用的“模式”之一是“服务对象”(有各种讨论和命名约定),因此,使用google这样您就可以获取更多信息。

This article is quite explanatory: https://www.engineyard.com/blog/keeping-your-rails-controllers-dry-with-services 本文颇具说明性: https//www.engineyard.com/blog/keeping-your-rails-controllers-dry-with-services

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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