i am looking into the possibilities of doing some work for a client on Flex with a Rails backend.
It ain't rocket science however i was wondering if anyone out there had some experiences with these two AWESOME technologies?
Thanks in advance....
Peter Armstrong has written an excellent book called Flexible Rails, and has also created a Flex/Rails framework called Ruboss. I recommend you check out both of these resources.
I have developed a few application using Flex and Rails. The easiest way is just to use the as3corelib (http://code.google.com/p/as3corelib/) and use standard JSON objects. You can play around with the AMF serializers, but none of them are production ready (they are written in ruby and not C).
The biggest thing you need to make sure of is if you are creating resources from Flex you need to send the authenticity key from flex. I do this by writing a helper to output it to a view:
<script language="Javascript">
function getAuthKey() { return "<%= form_authenticity_token %>"; }
</script>
Then use ExternalInterface to call it inside Flex.
ExternalInterface.call('getAuthKey')
Related
I'm creating a JavaScript web app. I want to use Ruby on Rails to provide the API to the data in JSON format. I am writing the front-end entirely in JavaScript that will make Ajax calls to the API. This way, I will have a consistent data API when I can write different apps on different platforms.
I'm new to Ruby on Rails and I'm not sure if this is a good approach. If so, could you help me point to a good tutorial to implement the back-end that I just mentioned? If not, please help suggest a good way.
Api example code : https://github.com/pigon-web-services/api-app/blob/master/app/controllers/location_controller.rb
How to use this api with javascript : http://pigon.ws/examples/jsonp?api=locationapi
I didn't work with it yet, but the Rails-API gem by spastorino might be helpful.
Also there is a Railscast on this, so you might want to check this out as a starting point
I am trying to write a sproutcore frontend for a rails 3.1 application I am making. I have had a look at the sproutcore guides, but I am interested in seeing real examples how to use sproutcore together with rails 3(.1).
I have found two examples, but each is totally different:
A todo-app created using bulk_api: an interesting approach using a specific REST-style to minimise the traffic. But it proposes to place the sproutcore app in app/sproutcore, and is still a bit unclear to me how that actually hooks in completely.
Travis-ci which seems to be a very clean example, rails 3.1, to use sproutcore. It is not yet completely clear to me, but all sproutcore js is cleanly stored inside app/assets/javascript/apps and as far as i can tell, the application.html just loads the js and provides the frame where everything is loaded into.
Do you know any other examples? How do you use sproutcore in your rails app?
The method you describe is the same way that you integrate backbone.js into a rails app, and it seems to work very well
https://github.com/codebrew/backbone-rails
This stores backbone in
app/assets/javascripts/backbone/
app/assets/javascripts/backbone/app/models
app/assets/javascripts/backbone/app/controllers
And then there is a script tag in the view that just initializes backbone
<script type="text/javascript">
$(function() {
// Blog is the app name
window.router = new Blog.Routers.PostsRouter({posts: <%= #posts.to_json.html_safe -%>});
Backbone.history.start();
});
</script>
I imagine a similar process for sproutcore would make sense
I did find a demo-project: sproutcore-on-rails that did manage to make things clearer for me.
David H.H. announced recently that jquery is going to be the default in Rails 3.1, and that Prototype helpers / RJS are going into a gem.
What does that mean for the future? Should we progressively forget about things like javascript helpers, RJS, and all these fun parts of rails? And start coding with jquery/json in mind?
I'm not against that at all, but I have to admit I find RJS really fun to use...
Yes, unobtrusive JavaScript has won. Many people write now directly JS / jQuery code in ERB templates. jQuery code to replace some DOM node with a partial is only slightly longer than equivalent RJS code. Regarding JS helpers, it should be quite easy to replace all of them with unobtrusive JS code. Check how data-remote is handled in jQuery Rails driver.
This trend makes sense because web apps have more and more JS code which cannot be written in RJS. It is better to use two languages than three.
If you don't like JavaScript syntax then check out CoffeeScript. Which will be definitely more popular than RJS.
Long term future probably belongs to pure JS frameworks like SproutCore. Server side will process only JSON data. However these new techniques will be used only in new apps. Generally it doesn't make sense to upgrade existing apps to the new model.
I have a webpage based on ruby on rails. For one task I need some more complex user interface which I plan to do with a flex programm. The question is, how I can start the a new window with my flex programm and hand over the right url where the flex app can load its data in XML format...
Thanks in advance
Markus
Remember that a Flex delivery is just a SWF file, with some extra magic around. You could just load it on a regular view or even a html file on the public directory and then have it talk with your Rails app.
We currently use Flex for charting some statistics and it was really painless to integrate. Since Rails favorites a RESTful approach, you can make your Flex app just call the right URIs for data and then work with the result (either JSON or XML).
I'm looking at Rails development as a backend to a Flex application and am trying to figure out the communication layer between the Rails app and the Flash Player. All of the things I am finding suggest using SOAP web services to communicate.
However, Flash supports AMF which is nice and fast (and native). Is there any way of communicating over AMF from a Rails app, whilst supporting all the "nice" things about AMF (automatic type conversion, data push etc).
There is WebORB or RubyAMF which you can use to respond in AMF from Rails, the approaches are a bit different for each one so it depends on your needs. RubyAMF is discussed in the closing chapters of the Flexible Rails eBook which is a good resource on using Rails with Flex.
I'm in the middle of writing a rails/flex application and we're moving to using a JSON communication within the REST framework. Simple HTTP requests from the Flex side handling JSON responses seemed like the best way to decouple the client and server. XML is just as easy.
For what it's worth, we're using the PureMVC framework on the flex side as well, keeping the responses in a client-side model.
You wouldn't use SOAP web services but rather 'native' REST web services, which are native in Rails. The book quoted by DEFusion above is actually about that: how to use a FLEX client as the front-end of a Rails application using REST (meaning XML).
The AMF protocol has primarily been built by Adobe as a binary protocol to allow FLEX front-ends to talk to CodeFusion and of course Java server applications. It's not free, apart from using Adobe's BlazeDS for which you actually won't have much support. And then of course, you'll have to choose a plugin capable of talking to BlazeDS using the AMF protocol (again, see DEfusion's posts) and rely on it.
You'd be surprised how well direct Flex to Rails via REST works, plus you don't have to rely on third-parties. I'd recommend you try it.
Hope this helps
Go with RubyAMF if you want an MVC style interaction with contollers that can respond to/generate AMF.
Use WebOrb for any other style, including direct access to model objects.
I've built apps using all three methods (WebOrb, RubyAMF, REST)...
WebOrb for Rails is pretty much dead, it hasn't been updated in quite sometime. That said I was able to create a bit of Flex/Ruby magic that makes Flex access to Rails' model objects transparent... if you're interested I'll dig it up and send it to you.
RubyAMF is nice, but not as Flexible (ha!) as WebOrb.
REST returning JSON is a snap, and if I have to build another one of these (I hope not) that's what I'll continue to use.
YMMV.
There's a Rails plugin called WebORB for Ruby on Rails which uses remoting with AMF.
You can use WebORB or RubyAMF, or just plain XML - Rails is pretty smart when it comes to XML, with a few gotchas here and there.
We use XML to speak between our Rails apps and our Flex web application, almost exclusively. It's pretty simple.
For retrieving data from your Rails app, just create an HTTPService with result_type of e4x, and call your url. In your rails controller, do something like:
def people
render :xml => Person.all.to_xml
end
Sometimes, Rails will add the tag to the end. If this happens, change your controller to:
def people
render :xml => Person.all.to_xml.target!
end
If you want to send data to your Rails app, it's just as easy..
<mx:HTTPService id="theservice" url="http://localhost:3000/svc/add_person" method="POST">
<mx:request>
<person>
<first>Firstname</first>
<last>Lastname</last>
</person>
</request>
</HTTPService>
and in your controller:
def add_person
p=Person.create(params[:person])
render :xml => {:result => "Success"}.to_xml.target!
end
Kevin