【问题标题】:Rail 3 - Passenger path Issue铁路 3 - 乘客路径问题
【发布时间】:2010-12-25 05:33:43
【问题描述】:

我的应用程序在需要文件的任何控制器或模型上返回并出错。 通常我需要一个如下所示的文件。

require '/lib/position_mover'

我玩了一下,如果我从下面显示的服务器的顶级目录中指定一个路径,它似乎可以工作。

require '/srv/www/testapp/lib/position_mover'

我想使用相对路径有很多原因。有人可以给我指导吗?

服务器配置:

  • 阿帕奇 2
  • Ubuntu 10.10
  • 导轨 3.0.3
  • 红宝石 1.9.2p0
  • mysql

虚拟主机:

<VirtualHost 173.255.238.220>
    ServerName test.targesoft.com
    DocumentRoot /srv/www/testapp/public/
    <Directory /srv/www/testapp/public/>
        PassengerAppRoot /srv/www/testapp/
        Allow from all
        Options -MultiViews
    </Directory>
</VirtualHost>

【问题讨论】:

    标签: ruby-on-rails apache ubuntu passenger


    【解决方案1】:

    如果您需要 Rails 应用程序的 lib 目录中的文件,则没有必要。默认情况下,Rails 需要其中的所有内容。

    【讨论】:

    • 遗憾的是不再在 Rails 3 中,您现在需要手动要求。
    • 或者你可以将它们添加到 autoload_path 设置中,当它们的常量被引用时,它们将被自动加载
    【解决方案2】:

    您需要将此模块放在lib 目录中,然后将其添加到config/application.rb 文件中的config.autoload_paths(默认情况下,该设置已被注释掉)。当你在代码中引用这个模块时,Rails 会自动知道需要 lib 目录中的文件。

    【讨论】:

      【解决方案3】:

      感谢您的回复!虽然插件是 lib 文件夹的事实,但我实际上在这里调用了我自己的自定义模块。我尝试从模型顶部取出 require 部分,但除非我需要它在我的文件顶部,否则我会收到未定义常量的错误。必须有一种方法来设置乘客,以便它默认在自己的目录中查找。

      module PositionMover
      
        def move_to_position(new_position)
          max_position = self.class.where(position_scope).count
          # ensure new_position is an integer in 1..max_position
            unless new_position.nil?
              new_position = [[1, new_position.to_i].max, max_position].min
            end
      
            if position == new_position # do nothing
              return true
            elsif position.nil? 
              increment_items(new_position, 1000000)
            elsif new_position.nil? 
              decrement_items(position+1, 1000000)
            elsif new_position < position 
              increment_items(new_position, position-1)
            elsif new_position > position
              decrement_items(position+1, new_position)
            end
            return update_attribute(:position, new_position)
          end
      
      
         def position_scope
          "1=1" 
         end
      
         def increment_items(first, last)
           items = self.class.where(["position >= ? and position <= ? AND #{position_scope}", first, last])
           items.each {|i| i.update_attribute(:position, i.position + 1)}
         end
      
      
         def decrement_items(first, last)
            items = self.class.where(["position >= ? and position <= ? AND #{position_scope}", first, last])
            items.each {|i| i.update_attribute(:position, i.position - 1)}
          end
      
      
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多