使用NeatBean ruby IDE 一(配置数据库和安装ruby,rails等插件)

Installing and Configuring Ruby Support

This document provides information about downloading, installing, and configuring Ruby support in the NetBeans IDE.

Contents

Downloading Ruby Support
Configuring the IDE to Use Your Own Ruby Installation (Optional)
Managing Gems
Using Database Servers With JRuby
Adding Databases
Registering Database Servers With the IDE
Deploying a JRuby Application to GlassFish
 Content on this page applies to NetBeans IDE 6.0

Downloading Ruby Support


If you do not have the NetBeans 6.0 IDE, go to the NetBeans IDE 6.0 Download Page, and download a version of the IDE that contains Ruby support. If you have a NetBeans IDE 6.0 installation that does not include Ruby support, complete the following steps to add Ruby support to the IDE.

  1. If your network uses a proxy, choose Tools > Options from the main menu, select Manual Proxy Settings, enter the HTTP Proxy and Port for your proxy, and click OK.
  2. Choose Tools > Plugins from the main menu.
  3. In the Plugins dialog box, click the Available Plugins tab, and scroll to the Ruby category, as shown below.

    Figure 1: Plugin Manager

    Plugin Manager
  4. Select the Ruby and Rails checkbox to get Ruby support for the NetBeans IDE.
  5. (Optional) Select the JRuby and Rails Distribution checkbox to download and install the JRuby software and the Ruby on Rails framework.
  6. (Optional) Select the Ruby Experimental Hints checkbox to include additional source code hints.
  7. Click Install.

    The NetBeans IDE Installer appears.
  8. In the NetBeans IDE Installer, click Next.
  9. Read the license agreement, then select the I Accept the Terms in All License Agreements radio button, and click Next.
  10. Click Install.
  11. After the installation completes, select either Restart IDE Now or Restart IDE Later and click Finish.

Configuring the IDE to Use Your Own Ruby Installation (Optional)


The first time that you create or open a Ruby project in the IDE, the IDE checks to see if you have any other Ruby installations in addition to the bundled JRuby software. If you do, the IDE displays a dialog box asking you to select which software to use.

If you decide to install the Ruby software later, you can complete the following steps to configure the IDE to configure the IDE to use your Ruby installation.

  1. In the IDE, choose Tools > Options from the main menu.
  2. Click Ruby, and, if necessary, click the Platform tab.

    If you have not yet opened or created a Ruby project, the first time that you access the Platform tab, the IDE displays a Choose Ruby Interpreter dialog box that shows a list of available Ruby installations, as show below. If the dialog box does not appear, skip to Step 5.

    Figure 2: Choose Ruby Interpreter Dialog Box

    Choose Ruby Interpreter dialog box
  3. Select the Ruby interpreter that you want to use. If you do not see your Ruby interpreter on the list, click Other.
  4. Click Close.
  5. If you chose Other in the previous step, or if the Choose Ruby Interpreter dialog box did not appear, click the Browse button, navigate to the location of your Ruby binary, and click Open.
  6. Click OK to close the Options dialog box.

For information about obtaining Ruby software, see www.ruby-lang.org, instantrails.rubyforge.org, and locomotive.raaum.org.

Managing Gems


Gems are third-party Ruby libraries. The IDE provides a Ruby Gems manager for adding and maintaining gems.

To open the manager choose Tools > Ruby Gems from the main menu. Click the Installed tab to view a list of the gems that that have been installed for the registered Ruby platform (See the previous section for information about switching the IDE Ruby Platform setting to use your own Ruby installation). If you are using the JRuby platform and the IDE includes the JRuby and Rails Distribution plugin, the list contains the ActiveRecord-JDBC gem and the Rails framework, as shown below. Click the New Gems tab to see what gems are available to add to your Ruby platform.

Figure 3: Installed Tab in Ruby Gems Manager

Installed tab in Ruby Gems manager

Note. The RubyGems wiki page provides information on how enable the Ruby Gems manager to manage a native Ruby gems repository.

For more information about Ruby Gems, go to www.ruby-lang.org/en/libraries/. For more information about Rails, go to rubyonrails.org. Information about the ActiveRecord-JDBC gem can be found at jruby-extras.rubyforge.org/ActiveRecord-JDBC.

Using Database Servers With JRuby


You can access databases from a JRuby on Rails application in one of two ways.

  • MySQL Adapter. The MySQL adapter is included with JRuby. You use this adapter just as you would with a native Ruby on Rails application.
  • JDBC Adapter. As mentioned earlier, the JRuby and Rails Distribution plugin includes the ActiveRecord-JDBC gem, which enables your JRuby on Rails application to access database servers that provide JDBC 3.0 compliant drivers, such as MySQL, PostgresSQL, Oracle, HSQLDB, and Java DB (also known as Derby). The JDBC driver must be a pure Java driver. At the time of this writing, ActiveRecord-JDBC does not work with the Sqlite JDBC driver.

    To use a JDBC connection, you must obtain a JDBC 3.0 client driver for your database server. For example, for MySQL, you can download the MySQL Connector/J driver. You must put a copy of the JDBC driver in the JRuby/lib folder. Depending on your installation, this folder can be in one of two places.

    • netbeans-install-dir/ruby1/jruby-1.0.1/lib
    • netbeans-user-dir/jruby-1.0.1/JRuby/lib

    To find the location of your JRuby installation, choose Tools > Options from the main menu, click Ruby, and click the Platform tab.

    Note: The Java DB database server is bundled with the Glassfish application server, and its derbyclient.jar is in the glassfish-install-dir/javadb/lib folder.

    To use the JDBC adapter in a Ruby on Rails project, select the Access Database Using JDBC checkbox when you create the project, as shown in the following figure.

    Figure 4: Selecting JDBC Database Access

    Selecting JDBC Database Access
    When you select this checkbox, the IDE puts the following statements in the environment.rb file.

    Code Sample 1: JDBC Environment Setup
    if RUBY_PLATFORM =~ /java/
      require 'rubygems'
      gem 'ActiveRecord-JDBC'
      require 'jdbc_adapter'
    end

    When you use the JDBC adapter to access a database, you configure the database settings in the database.yml just as you would if you were using that database's Rails adapter, as shown below.

    Code Sample 2: JDBC Database Configuration Example
      adapter: mysql
      database: my_development_database
      username: fred
      password: pfm
      host: localhost

    When you create a Ruby on Rails project, the IDE adds the configurations to the database.yml for the database server that you select in step 2 of the Wizard as shown in Figure 4. If you are using a database server that is not in the drop-down list, you can edit the database configuration in the database.yml file after you create the project.

Important Note: If your operating system's host file does not contain localhost, use 127.0.0.1 instead. Note also that with some systems, the database setting must be in lower case letters.

Adding Databases


Several of the NetBeans tutorials require that you create a database for the data tables that are used by the tutorial. The steps for adding a database are different for each database server. Below are instructions for the MySQL and Java DB databases.

Adding MySQL Databases

  1. If it has not already been started, start the database server.
  2. (Optional) Use the following steps to create a user.

    1. At a command prompt, type the following command.

      mysql -u user -p

      Omit the -p if the user does not have a password. Otherwise, when prompted, enter the password. The user must have global create user privileges or the insert privilege for the mysql database, such as the root user.
    2. Use syntax similar to the following example to create a user.

      CREATE USER 'fred'@'localhost'
      identified by 'pfm';
    3. (Optional) Use syntax similar to the following to specify access privileges. In this example, the user is granted all privileges to all databases on the localhost database server.

      GRANT ALL PRIVILEGES ON *.* TO 'fred'@'localhost';
    4. Type quit and press Enter to exit.
  3. To create a database, use syntax similar to the following at a command prompt.

    mysqladmin -u fred -p create mydb_dev

    Omit the -p if the user does not have a password. Otherwise, when prompted, enter the password. If you have not created users, you can use root as the username.

For more information about creating MySQL users and databases, see the MySQL Reference Manual.

Adding Java DB (Derby) Databases

If you installed a version of the IDE that includes the GlassFish application server, the IDE is configured to access the Java DB database that is provided with GlassFish. You can use the following steps to add a Java DB database from the IDE.

  1. From the main menu of the IDE, choose Tools > Java DB Database > Create Database.
  2. In the Create Java DB Database dialog box, enter the Database Name, User Name, and optional Password, as shown in the example below.

    Figure 5: Create Java DB Database Dialog Box

    Create Java DB Database dialog box
  3. To specify a different location for the database, click Settings. In the Java DB Settings dialog box, click the Browse button for the Database Location, navigate to the folder in which you want the database to reside and click Open. Click OK to close the Java DB Settings dialog box.
  4. Click OK. The IDE creates the database and adds a node to the Databases section in the Services window, as shown below.

    Figure 6: Database Node in Services Window

    Database node in Services window

Registering Database Servers With the IDE


You do not need to register your database server with the IDE to use it from your Ruby project. However, by doing so you can create and view tables, and exercise SQL commands directly from the IDE. Some of the NetBeans Ruby tutorials require that you register the database with the IDE.

Note: If you installed a version of the IDE that includes the GlassFish server, the Java DB database is automatically registered with the IDE.

The following steps show how to register the MySQL database server with the IDE and how to establish a connection from the IDE to a database instance.

  1. If you haven't already, obtain a JDBC 3.0 compliant driver for your database server, such as the MySQL Connector/J driver.
  2. In the Services window, expand Databases, right-click the Drivers node and choose New Driver from the pop-up menu.

    The New JDBC Driver dialog box appears.
  3. Click Add, navigate to and select the driver's jar file, and click Open.

    The New JDBC Driver dialog box displays the settings for the selected driver, as shown below.

    Figure 7: Selecting JDBC Database Access

    Selecting JDBC Database Access
  4. Click OK to close the New JDBC Driver dialog box.

    The IDE adds a node for your database server under the Drivers node, as shown below. You can now establish connections with MySQL database instances.

    Figure 8: Registered Drivers

    Selecting JDBC Database Access
  5. To create a connection from the IDE to a database instance, such as the example mydb_development database described in the previous section, right-click the Databases node and choose New Connection.

    The New Database Connection dialog box appears.
  6. Select your driver from the Name drop-down list, such as MySQL (Connector/J driver).
  7. Using the pattern shown in the Database URL text box as a guide, type the database's URL into the text box. For example, jdbc:mysql://localhost:3306/mydb_development.
  8. Provide the User Name, and provide the Password if it is required, as shown below.

    Figure 9: New Database Connection

    Selecting JDBC Database Access
  9. Click OK.
  10. When the connection is established, click OK to dismiss the dialog box.

    The IDE adds a node for the connection under the Databases node. The badge for the node shows that the connection is open. When you next start the IDE, the badge will be broken to indicate that the connection is closed. To reconnect, right-click the node and choose connect, provide your password if it is required, and click OK.

Deploying a JRuby Application to GlassFish


By default, your JRuby applications run in the WEBrick browser. If you want to deploy a JRuby application to the GlassFish application server, you can package the application and its dependencies in a web archive (WAR) file, and make the WAR file available to the application server, as shown in the following steps.

  1. The deployed application will use the production database. Open the database.yml file to ensure that the production configuration is set correctly.
  2. When you create your JRuby application, you are given the option to add rake targets to support application server deployment, as shown in the following figure. If you intend to deploy to GlassFish, you should select this checkbox.

    Figure 10: Option to Provide WAR Rake Targets

    Option to Provide WAR Rake Targets
    If you have an existing JRuby project that does not offer this rake target, complete the following steps to add the Goldspike plugin to the project. This plugin adds rake tasks that enable you to create WAR files.

    1. In the Projects window, right-click the project's node and choose Rails Plugins from the pop-up menu.
    2. In the Rails Plugins dialog box, click the New Plugins tab.
    3. Select the entry for Goldspike, and click Install.
    4. Click OK to start the installation.
    5. Close both the Installation and Rails Plugins dialog boxes.
    6. Right-click the project's node and choose Run Rake Task > Refresh List from the pop-up menu.
  3. To package your application in a WAR file, right-click the project's node and choose Run Rake Task > War > Standalone > Create.

    The IDE creates the WAR file and puts the file in the project's top folder.
  4. If you are using the JDBC adapter, put a copy of your database server's JDBC client driver in glassfish-install-dir/lib and start (or restart) the GlassFish server.

    At the time of this writing, you must use the JDBC adapter unless you are connecting to a MySQL database.
  5. Place a copy of the newly created WAR file into GlassFish's autodeploy folder. For example, copy projects-folder/MyRubyApp/MyRubyApp.war to glassfish-install-dir/domains/domain1/autodeploy.
  6. In a web browser, goto the URL for the application. For example, http://localhost:8080/MyRubyApp.

Next Steps


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值