SPRINGER BRIEFS IN COMPUTER SCIENCE Deepak Vohra JRuby Rails Web Application Development 123 SpringerBriefs in Computer Science Series Editors Stan Zdonik Peng Ning Shashi Shekhar Jonathan Katz Xindong Wu Lakhmi C Jain David Padua Xuemin (Sherman) Shen Borko Furht V.S Subrahmanian Martial Hebert Katsushi Ikeuchi Bruno Siciliano For further volumes: http://www.springer.com/series/10028 Deepak Vohra JRuby Rails Web Application Development Deepak Vohra dvohra09@yahoo.com ISSN 2191-5768 ISSN 2191-5776 (electronic) ISBN 978-3-319-03933-6 ISBN 978-3-319-03934-3 (eBook) DOI 10.1007/978-3-319-03934-3 Springer Cham Heidelberg New York Dordrecht London Library of Congress Control Number: 2013956201 © The Author(s) 2014 This work is subject to copyright All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer Permissions for use may be obtained through RightsLink at the Copyright Clearance Center Violations are liable to prosecution under the respective Copyright Law The use of general descriptive names, registered names, trademarks, service marks, etc in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made The publisher makes no warranty, express or implied, with respect to the material contained herein References to various copyrighted trademarks, servicemarks, marks and registered marks owned by the respective corporations and/or connected subsidiaries may appear in this book We use the names, logos, and images only in an editorial fashion with no intention of infringement of the trademark Printed on acid-free paper Springer is part of Springer Science+Business Media (www.springer.com) Abstract Ruby is one of the top 10 programming languages according to TIOBE index JRuby is Ruby for the JVM (Java Virtual Machine), a 100 % Java™ implementation of Ruby The essential difference between Ruby and JRuby is that Ruby runs on the Ruby interpreter and JRuby runs on the JVM JRuby lets you develop as Ruby and run as Java JRuby 1.7 supports Ruby 1.9 syntax (except some features) with compatibility level targeting Ruby 1.9.3 Java knowledge is not a pre-requisite to using JRuby Several commonly used websites such as LinkedIn™ make use of JRuby The objective of the brief is to discuss how to develop a Rails web application with JRuby What This Brief Covers? We introduce JRuby We install JRuby and run a simple script on the JVM We also invoke Java in a JRuby script We create a Rails application with JRuby We create scaffolding for a CRUD application We package the Rails application with warbler to a WAR file We deploy the application to JBoss™ application server and to Oracle™ WebLogic Server We run the JRuby application on WebLogic Server with Oracle™ Database 11g XE as the database We also run the JRuby application on JBoss application server with MySQL™ database as the database What You Need for This Brief? The brief requires JRuby, version 1.7 is used in the brief Java 1.7 is also required For running the Rails application JBoss AS server and Oracle WebLogic Server 12c are used For database, Oracle Database 11g XE and MySQL databases are used v vi Abstract Who Is This Brief For? The target audience of the brief is JRuby developers The brief is also for Java EE application developers who want to learn about how to package and deploy a JRuby application as a WAR file on a web server This brief is suitable for professional JRuby developers and also for an intermediate/advanced level course on JRuby Deepak Vohra Contents Installing JRuby 1.1 When to Use JRuby 1.2 Installing JRuby 1 Running a JRuby Script 2.1 Invoking Java from JRuby Setting the Environment for a JRuby Web Application 3.1 Creating a Oracle WebLogic Server Domain 11 12 Creating a JRuby Rails Application 4.1 Installing Rails 4.2 Installing ActiveRecord JDBC Adapter 4.3 Installing JRuby OpenSSL Gem 4.4 Creating a Rails Application 13 13 14 14 14 Configuring Databases for a JRuby Web Application 5.1 Configuring Oracle Database in Rails Application 5.2 Configuring MySQL Database in Rails Application 19 19 20 Creating Scaffolding for a JRuby Rails Application 6.1 Generating Scaffolding 6.2 The Controller Script 6.3 The Model Script 6.4 The View Scripts 21 21 21 24 24 Creating Database Tables by Running Migrations 7.1 Creating the Migration Script 7.2 Running the Migration Script 27 27 28 Precompiling CSS and JavaScript Assets 8.1 Precompiling Assets 31 32 vii viii Contents Enabling Flash 9.1 Creating the Sessions Database Table 35 35 10 Packaging the JRuby Application as a Java™ EE Web Application 10.1 Installing Warbler 10.2 Configuring Warbler 10.3 Generating the Web Application 37 37 37 39 11 Running the Web Application in Oracle™ WebLogic Server 11.1 Deploying the Web Application 11.2 Running the JRuby Web Application 41 41 42 12 Running the Web Application in JBoss™ Application Server 12.1 Deploying the Web Application to JBoss AS 12.2 Running the Web Application in JBoss AS 49 49 49 13 Using JDBC Data Source with a JNDI 13.1 Creating a Data Source JNDI in WebLogic Server 13.2 Configuring the Data Source in Rails Application 53 53 54 14 Fixing Common Issues in JRuby Web Application Development 14.1 CLOB Type Field 14.2 jruby.jar in Classpath 14.3 Adapter Not Specified 14.4 Driver Class and URL Not Specified 14.5 Driver Class Not in Classpath 14.6 Joda Time Version 14.7 Assets Precompilation 14.8 Ruby 1.9.2 Syntax 14.9 Session Store 14.10 Java Version 14.11 Production Database for WebLogic Server 14.12 Development Database for Rake Tasks 14.13 JSON Parameters 14.14 Flash Not Enabled 57 57 58 59 59 60 60 61 61 62 63 63 63 64 64 Index 65 Chapter Installing JRuby In this chapter, we will cover the following topics: • When to use JRuby • Installing JRuby First, we discuss when to use JRuby 1.1 When to Use JRuby “JRuby combines the convenience of scripting with the power of the Java platform.” (http://www.oracle.com/technetwork/articles/dsl/jruby-141877 html) As a Ruby developer if you want to take advantage of the benefits provided by the Java™ Virtual Machine such as high performance (runtime optimization of bytecode), real multithreading at the OS level with support for multi-core processors (in contrast to the green threads of Ruby, which run at the application level), and integration with the vast archive of Java libraries, JRuby is a suitable candidate A developer does not have to know any Java to develop a JRuby application A Rails application may be deployed to any of the application servers as a WAR file just as a Java EE application JRuby is platform independent which makes it easy to install and migrate JRuby is not suitable if using a lot of Ruby gems that require a native C extension, or if requiring some of the un-supported Ruby syntax 1.2 Installing JRuby In this section we install JRuby JRuby is available as an exe file that runs and starts an install wizard To run JRuby first we need to install JDK™ JRuby 1.7.0 requires JDK or later Download and install JDK from http://www.oracle.com/ technetwork/java/javase/downloads/index.html Set JAVA_HOME D Vohra, JRuby Rails Web Application Development, SpringerBriefs in Computer Science, DOI 10.1007/978-3-319-03934-3_1, © The Author(s) 2014 52 12 Running the Web Application in JBoss™ Application Server Fig 12.3 Displaying a catalog in JBoss AS Chapter 13 Using JDBC Data Source with a JNDI We used connection parameters driver, url, username and password in the database.yml to configure the production environment database Alternatively, we may configure the production database as a data source with a JNDI (Java™ Naming and Directory Interface) name in database.yml In this chapter we shall configure a data source in Oracle™ WebLogic Server and use the data source JNDI in the database.yml to connect to Oracle™ database 13.1 Creating a Data Source JNDI in WebLogic Server In the WebLogic Server select Services|Data sources Click on New|Generic Data Source to create a new data source as shown in Fig 13.1 Specify the JDBC Data Source Properties Specify a data source Name, a JNDI Name (jdbc/OracleDS) and select Database Type as Oracle Click on Next as shown in Fig 13.2 Select the default Database Driver Oracle’s Driver (Thin XA) and click on Next Select the default Transaction Options and click on Next In Connection Properties specify Database Name as XE, Host Name as localhost, Port as 1521, Database User Name as OE (or a different username) and the Password, and click on Next Driver Class Name, URL, Database USER Name, and Password get displayed Click on Test Configuration to test the connection A Connection test succeeded message indicates the connection gets established Click on Next The AdminServer is listed as the target server Click on Finish A data source gets configured as shown in Fig 13.3 D Vohra, JRuby Rails Web Application Development, SpringerBriefs in Computer Science, DOI 10.1007/978-3-319-03934-3_13, © The Author(s) 2014 53 54 13 Using JDBC Data Source with a JNDI Fig 13.1 Selecting New > Generic Data Source in WebLogic Server Admin Console 13.2 Configuring the Data Source in Rails Application To use the data source in the Rails application, configure the production database with the jndi parameter in database.yml production: adapter: jdbc jndi: jdbc/OracleDS Repackage the catalogs application with the warble war command Redeploy the application and run the web application with the same url http:// localhost:7001/catalogs/catalogs to display the same index page Instead of using a driver, url, username and password parameters we used the jndi parameter to specify a data source JNDI in database.yml Similarly, a data source JNDI may be configured in JBoss™ AS with MySQL™ database and the JNDI name specified in database.yml In Chap 14 we discuss some of the common issues in developing a JRuby web application and how to fix the issues 13.2 Configuring the Data Source in Rails Application Fig 13.2 Creating a data source in WebLogic Server Fig 13.3 A data source with a JNDI name in WebLogic Server 55 Chapter 14 Fixing Common Issues in JRuby Web Application Development In the previous chapters we discussed using JRuby to create a Rails application and deploy the application to Oracle™ WebLogic Server and JBoss™ application server In this chapter, we will cover the following issues that could occur in developing a JRuby Web application • • • • • • • • • • • • • • CLOB type field jruby.jar in Classpath Adapter not specified Driver class and URL not specified Driver class not in classpath Joda Time version Assets precompilation Key/Value pairs specified with ':' Session store Java™ version Production database for WebLogic Server Development database for rake tasks JSON Parameters Flash not enabled 14.1 CLOB Type Field One of the fields (the description field) for the catalogs table is of type CLOB If Rails 3.2.8 is used instead of Rails 3.1.8 and a new catalog entry created the following error gets generated NoMethodError in CatalogsController#create undefined method `unserializable_attribute?' # D Vohra, JRuby Rails Web Application Development, SpringerBriefs in Computer Science, DOI 10.1007/978-3-319-03934-3_14, © The Author(s) 2014 for 57 58 14 Fixing Common Issues in JRuby Web Application Development To fix the error remove Rails 3.2.8 with following commands gem uninstall rails -v 3.2.8 gem uninstall actionmailer actionpack activerecord activesupport activeresource rails -v=3.2.8 Download Rails 3.1.8 gem rails-3.1.8.gem from http://rubygems.org/gems/rails/ versions/3.1.8 Run the following command from the directory with the rails-3.1.8.gem file jruby -S gem install rails The issue occurs because the unserializable_attribute? is called from the file activerecord-jdbc-adapter-1.2.2/lib/arjdbc/ oracle/adaptor.rb unserializable_attribute? is defined in activerecord-3.1.x/lib/active_record/attribute_methods/ read.rb, but not in the 3.2.x version of read.rb The activerecordjdbc-adapter is not updated for Rails 3.2.x 14.2 jruby.jar in Classpath In packaging the Rails Application to a Web Application we used warbler to package the Rails application to a WAR file Warbler includes the jruby-complete jar, which is required for JRuby runtime in the WAR file If jruby.jar is also added in the runtime classpath of WebLogic Server the following error gets generated when the web application is run file:/C:/jruby1.7.0.RC2/lib/jruby.jar!/jruby/jruby.rb:11 warning: already initialized constant ClassReader ’ ERROR: application error org.jruby.rack.RackInitializationException: C:/Oracle/ Middleware/user_projects/domains/base_domain/servers/ AdminServer/tmp/_WL_user/catalogs/5k0r9w/war/WEB-INF/ app/controllers/catalogs_controller.rb:9: syntax error, unexpected ':'format.json { render json: @catalogs } The index.html.erb might generate one of the following error messages SyntaxError in Catalogs#index Showing C:/Oracle/Middleware/user_projects/domains/ base_domain/servers/AdminServer/tmp/_WL_user/ catalogs/5k0r9w/war/WEB-INF/app/views/catalogs/index html.erb where line #22 raised: C:/Oracle/Middleware/user_projects/domains/base_domain/ servers/AdminServer/tmp/_WL_user/catalogs/5k0r9w/war/ WEB-INF/app/views/catalogs/index.html.erb:22: syntax error, unexpected ':' 62 14 Fixing Common Issues in JRuby Web Application Development ');@output_buffer.append = ( link_to 'Destroy', catalog, confirm: 'Are you sure?', method: :delete );@ output_buffer.safe_concat(' ^ or undefined local variable or # method `confirm' for Replace ‘:’ with ‘=>’ in key/value pairs in catalogs_controller.rb, index.html.erb Replace ‘confirm’ with :confirm in index.html.erb Making the Ruby scripts syntax as pre Ruby 1.9.2 fixes the error The undefined local variable or method `confirm' is generated because the reference to confirm is non-specific Making confirm a symbol with the ‘:’ prefix assigns an internal representation to confirm Symbols that have been assigned to are treated as variables and un-assigned symbols are method calls 14.9 Session Store The session store is used to store flash messages while running and using the Rails application By default the session store is stored in the WEB-INF/config/initializers/session_store.rb script The default session store mechanism is cookie store specified with :cookie_store Cookie store is not the recommended method If cookie store is used as session store one of the following errors could get generated: ERROR: application error org.jruby.rack.RackInitializationException: undefined local variable or method `key' for main:Object or: