Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在RubyonRails中重构一些模型方法_Ruby On Rails - Fatal编程技术网

Ruby on rails 在RubyonRails中重构一些模型方法

Ruby on rails 在RubyonRails中重构一些模型方法,ruby-on-rails,Ruby On Rails,我的模型中有一些方法,用于访问视图中存储的文本哈希: class CarSpec < ActiveRecord::Base @@fuel_type_select_data = Hash[(1..5).to_a.zip(['Petrol', 'Diesel', 'Gas/petrol', 'Hybrid', 'Electric'])] @@mileage_type_select_data = Hash[(1..2).to_a.zip(['km', 'miles'])]

我的模型中有一些方法,用于访问视图中存储的文本哈希:

class CarSpec < ActiveRecord::Base

    @@fuel_type_select_data = Hash[(1..5).to_a.zip(['Petrol', 'Diesel', 'Gas/petrol', 'Hybrid', 'Electric'])]
    @@mileage_type_select_data = Hash[(1..2).to_a.zip(['km', 'miles'])]
    @@transmission_select_data = Hash[(1..3).to_a.zip(['Manual', 'Automatic', 'Tiptronic'])]
    @@wheel_drive_data = Hash[(1..3).to_a.zip(['Front', 'Rear', 'All'])]
    @@color_data = Hash[(1..8).to_a.zip(['black', 'white', 'beige', 
              'blue', 'yellow', 'green', 'red', 'silver'])]

    def text_for_fuel_type
       @@fuel_type_select_data[fuel_type] 
    end

    def text_for_mileage_type
       @@mileage_type_select_data[mileage_type] 
    end

    def text_for_transmission
       @@transmission_select_data[transmission] 
    end

    def text_for_wheel_drive
       @@wheel_drive_data[wheel_drive] 
    end

    def text_for_color
       @@color_data[color]
    end

    def text_for_interior_color
       @@color_data[interior_color] 
    end
class-CarSpec

目前,我需要为每个字段编写一个新方法。如何重构这些方法,使我不需要为每个字段编写新方法?请包括如何在视图中调用新方法。

在您的情况下,使用常量而不是类变量会更好

例如:

class CarSpec < ActiveRecord::Base
  WHEEL_DRIVE_DATA = {'Front' => 1}

  # remaining class code
end
class-CarSpec1}
#剩余类代码
结束
查看的示例代码:

<%= CarSpec::WHEEL_DRIVE_DATA['Front'] %>

同意@Humza:这里常量比类变量好

当前的方法集可以通过一些元编程魔法动态定义,如下所示:

fields = %w{ fuel_type mileage_type transmission wheel_drive interior_color }
fields.each do |field|
  define_method("text_for_#{field}") do
    CarSpec.class_variable_get("@@#{field}_select_data").send(:[], eval(field))
  end 
end 
注意: 为了使上述方法有效,所有字段的类变量必须与
@{field}\u select_data
名称一致;所以
@@wheel\u drive\u data
应该更改为
@@wheel\u drive\u select\u data
,等等