My previous post about getting Rails running was really only a pit-stop on the way to my real goal of getting my Android tablet to talk to a Rails server.
That's what I searched for first, which led me to the instructions for installing Rails on PC. So, to back up, I was looking for "Android and Ruby on Rails," and I got this post:
Which of course, references the other Google top search which is this:
So after figuring out how to get a Rails server running on PC (documented in my previous post), I decided to start with Gertig's demo project for Android (downloadable at the top of the link above).
Unfortunately, that did not work out of the box. Mainly because he only provides the source for the Droid app, not the Rails. So I jumped over to Michael Maitlen's blog (mgmblog link above) which Gertig referenced and took a look at his httpclient hello world, hoping that it would show me the Rails side of things.
Knowing nothing about Rails or Ruby(except what I gleaned from setting it up in my previous post), it took me a bit to puzzle through Maitlen's code and to realize that the commands he's using won't work if you've followed Gertig's instructions for setting up Rails on PC. If you have, here's what you need to do to create a Rails project for Gertig's demo Android project (note, I made changes to Gertig's demo, keep reading to find out what):
- DroidRails is the name of my project -
- Start Command Prompt with Ruby
- cd to your RailsApp folder
- code:>rails new DroidRails>cd DroidRails>rake db:create>rails generate scaffold events name:string budget:string>rake db:migrate[next, I did this in a new ruby cmd prompt, but you shouldn't have to]>rails server
Alright, I still know next to nothing about Rails, but from what I gathered, the above code creates a Rails project with a sqlite3 server (from when I setup Rails), adds a template (scaffolding?) for a SQL object called "events" with the entries "name" and "budget" which are both strings, and starts said server. From there, I can contact it with Gertig's demo Droid app.
Now, for the changes I made to Gertig's project to get it to work (this is required if you're going to create a server like I instructed, but I'm sure after reading, you'll be able to tell how to write a server to fit Gertig's project 100%):
- in AddEventView#postEvents()
- set the HttpPost to your local ip (note: my ip did not match Gertig's comments)
- change "budget" into a string and access just like "name" (I did this for simplicity's sake, I didn't want to worry about data types on the Rails or Droid to start)
- change "myevent" on line 85 to "event"
- in RubyMain#retreiveProjects()
- change the string url on line 68 to your ip (match HttpPost above)
- in RubyMain#parseXMLString()
- on line 136, change “myevent” to “event” for the nodes
- change “budget” to a string, update the set on line 153 to get a string like “name” does
Troubleshooting:
The three most likely culprits of any problem are 1) your Rails project isn't setup correctly, 2) your Droid project doesn't match your Rails project, and 3) your ip is incorrect.
If you followed my instructions above, 1 and 2 should not be a problem. If not, here's the main issues I ran into:
For 1:
- If you follow Gertig's Rails setup blog (which I followed in my previous post), and you try to build the Rails app by following Maitlen's instructions, you're doomed to fail.
- I found this site: http://guides.rubyonrails.org/getting_started.html helpful in figuring out how a Rails server should be built, and I compared the commands in it with Maitlen's to figure out what I needed to do.
For 2:
- If you followed my instructions for 1, then you'll need to follow all of them. In my Rails server, I changed “budget” to a string to further simplify the app. I also changed the name of “myevent” for no good reason.
- If you change the names, Rails does some interesting stuff automatically. To get my project working, I actually had to use Firebug to analyze the call made by the Rails test page that's generated for you (http://localhost:3000/events) in order to see what was being posted when I made a new event. Working backwards from that, I figured out how to alter Gertig's project to match. If something's not working, that's where I would start looking.[Note: one of the weirdest things here is that I used “events” in my server, but Rails used the singular form for creating a new “event.” It seems to have done this automatically.]
- Also, you need the right IP. Localhost works from your PC but doesn't from the Droid (because localhost on the Droid would refer to a server that it's running on itself). I cover the details of this below:
For 3, do the following:
- go to http://localhost:3000/
- make sure it shows the “Welcome aboard” Rails page. If it doesn't, your server isn't running.
- open a command prompt and type ipconfig, hit enter
- look at the list of IPs towards the top around “Default Gateway.” The one that worked for me was the “IPv4 address”
- go to http://[your IP address]:3000/
- make sure it also shows the Rails welcome page. If it doesn't, you've got the wrong IP address.
- Once you find the right one, add it to your Droid project.
Just some thoughts as a Rails developer also looking to building an android app:
ReplyDelete1/ Rails follows the convention of your model being in the singular and the database table being plural of that same. So, an Invoice model would match a table called invoices in the database.
2/ Be careful of using model names in Rails that would conflict as keywords in Android projects ? This I realized after reading your article. Anyone with pointers on this welcome to add.