Rail 3 - Passenger path Issue
My application returns and error on any controller or model that requires a file. typically I would require a file as shown below.
require '/lib/position_mover'
I played around with it a little bit and it seems to work if I specify a path from the top directory of my server show below.
require '/srv/www/testapp/lib/position_mover'
I want to use the relative path for many reasons. Can someone give me direction on this?
Server config:
- Apache 2
- Ubuntu 10.10
- rail开发者_如何学Pythons 3.0.3
- ruby 1.9.2p0
- mysql
Virtual host:
<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>
If you're requiring a file inside the lib
directory of a Rails app, that's not necessary. Rails requires everything in there by default.
You'll want to place this module in the lib
directory and then add it to config.autoload_paths
in your config/application.rb
file (a setting which, by default, is commented out). When you reference this module in your code, Rails will automatically know to require the file in the lib
directory.
Thanks for the response! While plugins are the defacto for the lib folder, I'm actually calling in my own custom module here. I tried taking the require piece out from the top of the model, but unless I require it a the top of my file, I get an error for an undefined constant. There must be a way to set up passenger so that it looks in it's own directory by default.
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
精彩评论