2

I am trying to build a Ruby on Rails web application with PostgreSQL database and a Java application with H2 Database Engine using Hibernate.

I have heard about the JDBC, RMI and Sockets but i have no knowledge about any of those.

The problem is that, i would like to use data from the web application, do some processing in it using local database and finally i would like to upload changes to the database or let's say i want to be to create new tables in the web database and immediately show the changes in the web.

For example, i would like to make students register in web, take the students roll no from web database to local database and enter the marks and finally i would like to be able to add marks in web database and in the web the individual marks should be published immediately.

I would be very grateful if any one could help me.

2 Answers 2

2

The solution to your problem is a (REST) API.

I would suggest to create separate controllers for your API, under an api/v[number_of_version]/

There is a very good example at http://railscasts.org by Ryan Bates on how to build a REST API here.

You can find code example for an API here.

https://github.com/railscasts/350-rest-api-versioning/tree/master/store-after/app/controllers/api

Then you can fetch data with HTTP requests.

eg. to fetch data for a user in your app you will do an http request in the url (that you will define in your routes.rb under the api and version namespace) /:username

eg http://yourdomain.com/ausername

and you will have a JSON response like

user: {
  username: "ausername"
  email: "[email protected]"
 .....

Don't hesitate to ask me if you have any questions :)

Update for QUESTION

Of course you can add data this way. You will just have to export the correct json to the correct url.

I have done an example in this app

https://github.com/Geembo/getshitdone

the example:

https://github.com/Geembo/getshitdone/blob/master/app/controllers/tasks_controller.rb

In this app I save the tasks through Javascript (Backbone). The same rules apply to Java (and any other language)

Sign up to request clarification or add additional context in comments.

1 Comment

Will i be able to add data and create new tables using these methods from the java application?
0

The best way to handle this is to provide an API from the web application that external applications can interface with. One great thing about Rails is it basically supports this out of the box.

Take a typical controller action for viewing a student from the web.

class StudentsController < ApplicationController
  def show
    @student = Student.find(params[:id])
  end
end

When the request is made to /students/1 then the students/show template will be rendered for Student with ID#1.

Rails has the ability to append xml/json to the end of the URL in order to request processing with a different format type.

class StudentsController < ApplicationController
  respond_to :html, :json

  def show
    @student = Student.find(params[:id])
    respond_with(@student)
  end
end

This sets up a feature of ActionController called the Responder. Now when a request is made to /students/1.json the controller will call as_json on the Student model, which by default takes all of the models attributes and converts it to a json object. This can be customized by overriding as_json in your Student model.

For making updates you can follow a similar pattern. You submit to the server using a PUT request to /students/1.json. The request is not in JSON, the library you use likely has support for setting the vars, make sure it is in the format that Rails expects (ie student[field]). The Rails middleware will take care of the rest.

class StudentsController < ApplicationController
  respond_to :html, :json

  def update
    @student = Student.find(params[:id])
    @student.update_attributes(params[:student])
    respond_with(@student)
  end
end

Note that with Responders there is no checking if update_attributes worked, the respond_with will do that for you. If there are errors you will get a HTTP 422 Unprocessable Entity as a response code, and the body of the response will be a JSON object with the errors.

It's also worth mentioning that you can substitute json with xml in all of these examples if you prefer XML response bodies instead of JSON.

3 Comments

Does it mean i can only edit, update and delete the existing data. Can i create a new table using these methods?
A new table? No, you can create new records with the existing model if that's what you mean.
Ok. A new record will be fine. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.