错误:方法体中的类定义和动态常量赋值PLAN_POLICIES = [

时间:2017-05-24 16:17:09

标签: ruby-on-rails

我尝试应用了一些代码:

  class Guest

attr_accessor :custom0, :custom6, :custom7, :custom8

def initialize(args)
  args.each do |k,v|
    self.send("#{k}=",v)
  end
end

end

PLAN_POLICIES = [
{number_of_guests: 10, type_of_guest: "MP PLATINO", plan_id: 64},
{number_of_guests: 10, type_of_guest: "MP", plan_id: 53},
{number_of_guests: 9, type_of_guest: "MP PLATINO", plan_id: 63},
{number_of_guests: 9, type_of_guest: "MP", plan_id: 52},
{number_of_guests: 8, type_of_guest: "MP PLATINO", plan_id: 62},
{number_of_guests: 8, type_of_guest: "MP", plan_id: 51},
{number_of_guests: 7, type_of_guest: "MP PLATINO", plan_id: 61},
{number_of_guests: 7, type_of_guest: "MP", plan_id: 50},
]


def plan_id
return PLAN_POLICIES.find do |pp|
  pp[:number_of_guests] == number_of_guests && pp[:type_of_guest] == type_of_guest
end[:plan_id]    
end

 def number_of_guests
@guest.custom6.to_i + @guest.custom7.to_i
end

def type_of_guest
return "MP" if @guest.custom0 == "MP"
return "MP PLATINO" if @guest.custom8 == "MP PLATINO"
end


@usage_plan = UsagePlan.find_by_id(plan_id)

但是我收到了这个错误: “方法体中的类定义”

“动态常量赋值   PLAN_POLICIES = [“

以下是完整代码:Full Code Here

提前感谢您的帮助!

Besth Whises!

1 个答案:

答案 0 :(得分:0)

好。首先,constant声明(按照惯例,在所有大写中)需要在你的类声明之下(从不在方法定义中,就像你一样),如下所示:

  class Portal::RoseController < PortalController

    PLAN_POLICIES = [
      {number_of_guests: 10, type_of_guest: "MP PLATINO", plan_id: 64},
      {number_of_guests: 10, type_of_guest: "MP", plan_id: 53},
      {number_of_guests: 9, type_of_guest: "MP PLATINO", plan_id: 63},
      {number_of_guests: 9, type_of_guest: "MP", plan_id: 52},
      {number_of_guests: 8, type_of_guest: "MP PLATINO", plan_id: 62},
      {number_of_guests: 8, type_of_guest: "MP", plan_id: 51},
      {number_of_guests: 7, type_of_guest: "MP PLATINO", plan_id: 61},
      {number_of_guests: 7, type_of_guest: "MP", plan_id: 50},
    ]
    ...
  end

并且,您想要完成该列表,因为它不完整。

然后,你有这个:

  def pms_guest_purchase
    super
    # if there is a free plan match, then we want to pick the most appropriate
    # plan and automatically purchase it.  For now, we will just select the one
    # most appropriate plan.

    class Guest

      attr_accessor :custom0, :custom6, :custom7, :custom8

      def initialize(args)
        args.each do |k,v|
          self.send("#{k}=",v)
        end
      end

    end
    ...
  end

但是,正如我在其他帖子中所说,Guest仅仅是为了这个例子。所以,完全摆脱它。

随着那种消失,你现在有了这个:

  def pms_guest_purchase
    super
    # if there is a free plan match, then we want to pick the most appropriate
    # plan and automatically purchase it.  For now, we will just select the one
    # most appropriate plan.


    def plan_id
      return PLAN_POLICIES.find do |pp|
        pp[:number_of_guests] == number_of_guests && pp[:type_of_guest] == type_of_guest
      end[:plan_id]    
    end

    def number_of_guests
      @guest.custom6.to_i + @guest.custom7.to_i
    end

    def type_of_guest
      return "MP" if @guest.custom0 == "MP"
      return "MP PLATINO" if @guest.custom8 == "MP PLATINO"
    end

    ...
  end

但你从不在类似的方法中声明方法。所以,把它们移出来,就像这样:

  def plan_id
    return PLAN_POLICIES.find do |pp|
      pp[:number_of_guests] == number_of_guests && pp[:type_of_guest] == type_of_guest
    end[:plan_id]    
  end

  def number_of_guests
    @guest.custom6.to_i + @guest.custom7.to_i
  end

  def type_of_guest
    return "MP" if @guest.custom0 == "MP"
    return "MP PLATINO" if @guest.custom8 == "MP PLATINO"
  end

  def pms_guest_purchase
    super
    # if there is a free plan match, then we want to pick the most appropriate
    # plan and automatically purchase it.  For now, we will just select the one
    # most appropriate plan.

    @usage_plan = UsagePlan.find_by_id(plan_id)

    if @guest 
      ...
    end

  end

了解您如何拥有if @guest声明?在那里移动@usage_plan = UsagePlan.find_by_id(plan_id)。如果没有type_of_guestnumber_of_guests@guest会失败。而且,如果这是我的代码,我会制作那些辅助方法private。所以,你最终得到了这个:

  class Portal::RoseController < PortalController

      PLAN_POLICIES = [
        {number_of_guests: 10, type_of_guest: "MP PLATINO", plan_id: 64},
        {number_of_guests: 10, type_of_guest: "MP", plan_id: 53},
        {number_of_guests: 9, type_of_guest: "MP PLATINO", plan_id: 63},
        {number_of_guests: 9, type_of_guest: "MP", plan_id: 52},
        {number_of_guests: 8, type_of_guest: "MP PLATINO", plan_id: 62},
        {number_of_guests: 8, type_of_guest: "MP", plan_id: 51},
        {number_of_guests: 7, type_of_guest: "MP PLATINO", plan_id: 61},
        {number_of_guests: 7, type_of_guest: "MP", plan_id: 50},
      ]

    ...


    def pms_guest_purchase
      super
      # if there is a free plan match, then we want to pick the most appropriate
      # plan and automatically purchase it.  For now, we will just select the one
      # most appropriate plan.

      if @guest 
        @usage_plan = UsagePlan.find_by_id(plan_id)
        ...
      end

    end

    ...

  private

    def plan_id
      return PLAN_POLICIES.find do |pp|
        pp[:number_of_guests] == number_of_guests && pp[:type_of_guest] == type_of_guest
      end[:plan_id]    
    end

    def number_of_guests
      @guest.custom6.to_i + @guest.custom7.to_i
    end

    def type_of_guest
      return "MP" if @guest.custom0 == "MP"
      return "MP PLATINO" if @guest.custom8 == "MP PLATINO"
    end

  end

另外,我没有对此代码库有多少控制或影响,但rose_controller WAAAAY太胖了。应该将一大堆东西移到普通的旧红宝石物体中。 (如果你愿意的话,我们可以单独做一个Q&amp; A。)

FWIW。

祝你好运,如果你有问题,请告诉我。