In this blog we will create a Rhodes application which will synchronize your data from rails application through Rhosync.
We need to create 3 applications
- Rails Application: It will feed data in json or XML format.
- Rhodes Application: This code will be installed in mobiles.
- Rhosync Application: These will sync the data from rails App to Rhodes Application in your phone
Steps to create rails Application:
I will just create a rails application using scaffold:
- rails employee_details
- cd employee_details
- Ruby script/generate scaffold employee name:string phno:integer address:text
- Update your database.yml
- Rake db:migrate
- Add these code in your employees controller (you can skip this step if you want to only show employee details and does not do crud operations.)
protect_from_forgery :except => [:create, :update, :delete]
7. Add this line under response in controller:
format.json { render :json => @employee }
8. Once it is done we will run it on port 3000:
http://localhost:3000\employees.json
we will get output in json format.
Steps to create Rhodes application:
- Rhogen app employee
- Cd employee
- Rhogen model employee name,phno,address
- Add link in index.erb to your index page
- Enable sync in model Employee
6. Edit rhoconfig.txt file and set
syncserver = 'http://localhost:9292/application
7. rake run:bb
Steps to create Rhosync application:
- Rhosync app employee_server
- Rhosync source employee
- Then employee.erb incude json and rest client
require 'json'
require 'rest_client'
4. Update the query method
def query(params=nil)
parsed=JSON.parse(RestClient.get("http://localhost:3000/employees.json").body)
@result={}
parsed.each { |item|@result[item["employee"]["id"].to_s]=item["employee"] } if parsed
@result
end
Now you can test this by login to your simulator.
can this work for any simulator
ReplyDeleteRails and Rhosync application will be same for all devices.
ReplyDeleteIn Rhodes application
> for Android/ Iphone U have to add link in index.erb and for Blackberry add link in index.bb.erb
For Android instead of using localhost use your ipaddress because android simulator in not able to connect with localhost.
Hi i am new to rhodes , i am not getting your step 4 . Add link in index.erb to your index page . what it means , what is index page here . can u give the example line .
ReplyDeleteIf you start your Rhodes application by default index page(This file is in app folder) is loaded. We have to add a link to Employee index page. If you are using Iphone or Andorid you have to add the link in index.erb and if you are using Blackberry do it index.bb.erb.
ReplyDeleteThanks for response i got your point and successfully implemented this tutorial on my local system . I liked your tutorial, Thank you very much again.
ReplyDeletehi, abhishek
ReplyDeleteWhen i logging in with first user(say A) to my client the sync process works fine(all data sync properly to device.) . but when after logged out of A ,when i tried to login with another user(B) the sync process does not seems to work. Is there any restriction for sync server that on one client is dedicated to only one user that has been logged first after installation of app on device . If not then what i am doing wrong please let me know . my environment is "Development" and data partition is "user" . Thanks in advance.
Hi Raj,
ReplyDeleteIf you have not implement any authentication then it should work. You have written the sync code in query method right?(If your source adapter is user then put your sync code in query method of user.rb)
Note: If you have access Rhosync for more then 10 different user then you have to reset user from rhosync web console.
Hi abhishek,
ReplyDeleteThere is no authentication on user ,also i wrote all the sync adapter correctly(that is why it is working for user A). My rhosync server has only two users .
After logout of User A , when i reinstalled the app and tried to login with user B then sync data is working fine for user B as well . I am not sure whether i have to reset the user preference and delete the existence of first user from the client on call of "on_sync_user_changed" . If is this the case then please tell me how to clear my first user existence from the client before login with other user.
I think it should have work. I checked, it is working good in my application.
ReplyDeleteHowever you can right following line in logout action:
Rhom::Rhom.database_fullclient_reset_and_logout
No luck abhishek .Not working ..
ReplyDeleteI have a question regarding data base query in rhodes app . I have one model called Element. When i doing query as :
ReplyDeleteElement.find(:all,:conditions=>
{
{
:name => "form_id",
:op =>"IN"
} => [1,2,3]
}
)
above query works fine (Here form_id is one of column in Element model).
But when i tried to search all the element on basis of :object column . always it return [] , although data is available corresponding these objects i.e
Element.find(:all,:conditions=>
{
{
:name => "object",
:op =>"IN"
} => [4,5,6]
}
)
What i am doing wrong . Is the any other way to above query .
I also tried this is not working.. Even I tried :
ReplyDeleteElement.find(:all,:conditions=>
{
{
:name => "object",
:op =>"="
} => "4",
{
:name => "object",
:op =>"="
} => "5",
{
:name => "object",
:op =>"="
} => "6",
} ,
:op =>"or"
)
This also is not working for object but working for other fields. I think you are also using property bag..This seem a bug for property bag schema...
I have raised this issue on Rhomobile groups http://groups.google.com/group/rhomobile/browse_thread/thread/6aa1415b2c166415
Let see if they can find out some solution for us.
right, abhishek its seems to be bug for propertybag.
ReplyDeleteThanks for raising this issue to rhomobile group . I am waiting for responses.
Do you have any idea how to use Multiple select box in rhodes app . If you know the syntax then please let me know . Following syntax is not working.
ReplyDeleteMultiple select box is HTML part.It is not releated to Rhodes. So If the device support that HTML/JS then Rhomobile also support that.
ReplyDeleteWhich device you are using? If you are using Blackberry it has lot limitation which you can check at: http://docs.blackberry.com/en/developers/deliverables/1143/browser_devguide.pdf
You are right but all other HTML control is working for me except multiple select box .I am using android . Also In the rhodes Doc section i did not find any thing about multiple select box.
ReplyDeleteWhat code you are using for multiple select box?
ReplyDeleteIf you don't have much options you can use checkbox.
I am using simple HTML code for multiple select box.
ReplyDeleteI think there is issue with Multiple select box for Android:
ReplyDeletehttp://stackoverflow.com/questions/5654140/html-select-multiple-in-android-renders-weird
Yes Abhishek , there is issue in android with multiple select , temporarily i used checkbox as alternative.
ReplyDeleteHi Abhishek ,
ReplyDeleteI am submitting a form , I has different types of controls like date filed , time field , camera , signature and many more . My issue is that when filling the form if control goes out from the form(i.e if i opt to select time or date or signature) ,the form refreshes and all the filled data in form get disappear(user has to fill it again) . In rhodes how can i retain form data in controls if control go out of form.
Please provide your inputs.
Hi Raj,
ReplyDeleteI got your point.. It will happen as we are rendering to another page. I can think of two solution for this :
1.) You store all parameters in dummy table.
2.) Pass the values of form to another page as hash of parameters. You have to use lot of JavaScript for this.
Hi Raj
ReplyDeleteThe issue which you pointed out for object:
Element.find(:all,:conditions=>
{
{
:name => "object",
:op =>"IN"
} => [1,2,3]
}
)
This issue has been fixed and will release with Rhodes version3.1
Good job you pointing out this issue :)
Hi Abhishek,
ReplyDeleteI am using same technique to retain data in form controls ,off-course there is lots of javascript work for this.
About query issue ,it is good that this problem has been fixed for next release ,do you have any idea when next release is planned for rhodes 3.1 .
Hi Abhishek,
ReplyDeleteCan you help me, I am new in Ruby and Rhomobile
Just started your book and cannot emulate Rails app,
I have created app and models, but json output is different
so rhodes app doesn`t take it.
Here is Rails generated model Controller part:
def index
@companies = Company.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @companies }
end
end
My json output:
[{"name":"Tito","created_at":"2012-05-21T15:22:33Z","updated_at":"2012-05-21T15:22:33Z","id":1},{"name":"Noa","created_at":"2012-05-21T15:22:42Z","updated_at":"2012-05-21T15:22:42Z","id":2},{"name":"Afcto","created_at":"2012-05-21T15:22:50Z","updated_at":"2012-05-21T15:23:08Z","id":3},{"name":"Linux","created_at":"2012-05-21T15:22:58Z","updated_at":"2012-05-21T15:22:58Z","id":4},{"name":"Nokia","created_at":"2012-05-23T10:26:01Z","updated_at":"2012-05-23T10:26:07Z","id":5}]
Your sample output is:
[{"company":{"name":"Apple","created_at":"2012-04-23T09:04:24Z","updated_at":"2012-04-23T09:07:21Z","id":134}},{"company":{"name":"Google","created_at":"2012-04-22T03:00:26Z","updated_at":"2012-04-22T03:00:26Z","id":102}},{"company":{"name":"Vicco Tee","created_at":"2012-04-24T18:44:43Z","updated_at":"2012-04-24T18:45:22Z","id":143}},{"company":{"name":"Nokia","created_at":"2012-05-01T11:55:00Z","updated_at":"2012-05-01T11:55:00Z","id":159}}]
@Artur Instead of changing in Rails you can make small change in Rhosync/Rhoconnect to make it work. You change this line
Deleteparsed.each { |item|@result[item["company"]["id"].to_s]=item["company"]
by
parsed.each { |item|@result[item["id"].to_s]=item
Hi Abhishek,
ReplyDeleteThank you very much it worked.
But can you recall how your app was made and what is difference between mine and yours, why different output?
One more question, why rhoconnect needs some app to feed him info, can it directly connect to some db?
Thank you
Artur
I have used normal Scaffold to generate a rails app. Syntax for your code also seems fine.
DeleteYes you can directly connect Rhoconnect with some db in fact you can use redis as your database.
wonderful tips you have given..please provide a sample login module in which Rhodes app has to login to the backend application(Rails) and then only user can access Rhodes application..(i.e userid and pass is avail on server)
ReplyDeletei've read the tutorial i.e u have to change in authenticate(username,password,session) fro m application.rb in rhoconnect app. but am not getting the flow of data so i couldnt execute it..please provide a step vise procedure to execute it
awesome post presented by you..your writing style is fabulous and keep update with your blogs Ruby on Rails Online Course Bangalore
ReplyDelete