简体   繁体   English

在来宾linux virtualbox上运行rails:一个database.yml问题

[英]running rails on guest linux virtualbox: a database.yml issue

a) I'm a ruby in rails beginner developer, and I use windows 7 machine as developement environment... a)我是Rails初学者开发人员中的红宝石,并且我使用Windows 7机器作为开发环境...

b) With VirtualBox I just installed, inside the Windows 7 "host", a Linux ubuntu sever "guest", just to run the rails DEVELOPMENT environment ALSO in the linux machine. b)使用VirtualBox,我刚刚在Windows 7“主机”中安装了Linux ubuntu服务器“ guest”,仅用于在Linux机器中运行rails开发环境。

c) To do that I configured a virtualbox SHARED FOLDER: let say I have this shared folder on the host machine (window): c)为此,我配置了一个virtualbox SHARED FOLDER:假设我在主机(窗口)上有此共享文件夹:

c:\\rails\\esamiAnatomia c:\\ rails \\ esami解剖

and mounted it on the linux embedded server: 并将其安装在linux嵌入式服务器上:

/home/solyaris/host/esamianatomia / home / solyaris / host / esamianatomia

d) In this exptended "developement environment" I would like to edit source files with my preferred visual editor on windows (sublime text) and run rails server on linux. d)在这个扩展的“开发环境”中,我想在Windows(崇高的文本)上使用首选的可视化编辑器编辑源文件,并在Linux上运行Rails服务器。

The problem concern database.yml configuration file: 问题涉及到database.yml配置文件:

/home/solyaris/host/esamianatomia/config/database.yml /home/solyaris/host/esamianatomia/config/database.yml

because on Windows I have a database (postgresql) responding port 5433, with specific username/password 因为在Windows上我有一个数据库(postgresql)响应端口5433,具有特定的用户名/密码

but in linux database respond to port 5432, etc. 但在linux数据库中,响应端口5432等。

Questions: 问题:

1) It's that "arcgitectural solution ok ? (I mean: developing/editing from a windows 7 host, but running rails server of the linux guest server); 1)就是“建筑解决方案好吗?(我的意思是:从Windows 7主机开发/编辑,但运行Linux客户服务器的Rails服务器);

2) There is a way to change/configure database.yml on the fly (I mean: using two different database.yml files: one for the linux machine and anotherone for the window machine) ? 2)有一种动态更改/配置database.yml的方法(我的意思是:使用两个不同的database.yml文件:一个用于linux计算机,另一个用于窗口计算机)?

thanks a lot giorgio 非常感谢乔治

You can technically accomplish 2 if you're not afraid to play around with the guts of Rails. 如果您不惧怕Rails的胆量,您可以从技术上实现2。 As with any solution that has you accessing internal rails components this may stop working at any time, but fortunately this part of the API is not likely to change often, if ever. 与访问内部Rails组件的任何解决方案一样,它可能会随时停止工作,但幸运的是,API的这一部分不太可能经常更改(如果有的话)。 Still, use this at your own risk. 不过,使用此方法后果自负。

Here's how I do it on my projects. 这是我在项目中执行的方法。 First modify your application as follows: 首先,如下修改您的应用程序:

# config/application.rb:

# After require 'rails/all'
require_relative 'db_override'

Then create this new file: 然后创建这个新文件:

# config/db_override.rb:
case Socket.gethostname
when 'host1'
  $db_config = 'config/host1_database.yml'
when 'host2'
  $db_config = 'config/host2_database.yml'
else
  $db_config = nil # Use the default config/database.yml
end

if $db_config
  class DBConfigSelect < Rails::Railtie
    initializer "db_config_select", before: "active_record.initialize_database" do
      puts "Using custom DB configuration: #{$db_config}"

      # Get the existing path configuration
      cur_paths = Rails.application.config.paths['config/database'].instance_variable_get :@paths

      # Override the default config sources
      cur_paths.shift
      cur_paths.push $db_config
    end
  end
end

What you are describing is pretty much the setup that Vagrant is offering, so yeah, you are doing fine, everyone else is also doing it but they didn't set it up themselves (and by that probably get some really nice addons as well, you should take a look at Vagrant). 您所描述的几乎是Vagrant提供的设置,是的,您做的很好,其他所有人也都在做,但他们自己没有进行设置(这样可能还会得到一些非常不错的插件,您应该看看Vagrant)。

For your second question: no. 关于第二个问题:不。 Not on the fly. 不能飞。 Rails loads the database.yml end then connects to the database with that. Rails加载了database.yml端,然后以此连接到数据库。 When you change it while your Rails server is running, the changes won't get noticed. 当您在Rails服务器运行时进行更改时,所做的更改不会引起注意。 What you can do however is setup a new environment for your two different machines. 但是,您可以为两台不同的计算机设置一个新环境。 Then you can switch between the different environments and depending on the environment, one or the other database gets accessed. 然后,您可以在不同的环境之间切换,并且可以根据环境访问一个或另一个数据库。

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

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