How do I use rails helper in an emberjs application - ruby-on-rails

I want to use rails helper method, for example I want to use form_for or root_url or devise's sign_in?, how could I do that?

Using Rails helpers inside your Ember application isn't really something that is generally feasible. You can get creative and do it by dynamically generating JS server-side, but it's not something I would consider best practice.
I could imagine it being useful to export some set of your Rails routes as JS that the application could use.

The short answer is, you can't, at least not "out of the box".
The longer answer is... any kind of tight coupling like this between Rails and Ember would ultimately be bad for both frameworks if it was included by default.. you'd be forced to use Ember with Rails, and vice-versa. As it stands, Ember is pretty agnostic about your server backend, as long as it returns a proper JSON response Ember doesn't care if it's Ruby (Rails or Sinatra typically), PHP, Node.js or even static .json files. That gives you a lot of flexibility when building your app, but it also makes it impossible to assume things like the Rails router (or the Rails form_for helper) should exist.
If you want something like a form_for helper, your best bet would be to either write it as a Handlebars helper or (better option in my opinion) a custom View class and a handful of Handlebars helpers to give you most of what the Rails route helpers give you.
For the route helpers, you'll want to find an automated way to export your actual Rails routes to Javascript, then go from there. For a good starting point, checkout this question
Accessing rails routes in javascript

Related

Using AngularJS in Ruby on Rails app

I have a existing project in Ruby on Rails.
What is the best way to use AngularJS in Ruby on Rails app?
I want to use AngularJs for only specified modules, not for create SPA. This is good way?
What I have seen colleagues do in order to achieve this sort of integration with an existing rails app is:
Include angular.js and relevant dependencies in the specific app pages that are to be 'angularized'
Interpolate whatever data is needed to bootstrap the angular controller into the html template which contains the angular app. This might include data about the resource being operated on. I've seen this done by rendering a RABL template inside of a haml/erb template.
Using that interpolated data, call whatever API methods you need to get additional data on the fly. This is usually just a matter of implementing json handlers for routes you've already created.
I can't say whether this is best practice, but its an easy way to get started fast.
Best of luck, angular is a very enjoyable tool to work with once you get used to it.

Backbone & Rails. Why do you set routes in backbone?

js and I want to create a rails app with backbone. The only problem is, I cant seem to figure out what goes in the backend and what goes in the frontend. The approach I am using is to use rails essentially as an API.
I am looking at various different approaches and I cant seem to quite understand why you route in backbone as well as using normal rails routing.
My theory is:
You use rails to display initial html pages, and you use backbone routing to route the javascript files to that html pages so you can perform DOM manipulation there.
Here is the part of my code where I got that idea from. (NOTE: this is all mainly taken from Ryan Bates railcast on backbone.js)
backbone router
class Poster.Routers.Posts extends Backbone.Router
routes:
'':'index'
'posts/:id': 'show'
initialize: ->
#collection = new Poster.Collections.Posts()
#collection.fetch({reset: true})
index: ->
view = new Poster.Views.PostsIndex(collection: #collection)
$('#index_container').html(view.render().el)
views/main/index.html.erb
<div id="index_container"></div>
So does this backbone routing essential do the equivalent of just loading the javascript in the index: method at the top of the views/main/index.html.erb? (i know actually copying those javascript lines into the index.html.erb file wont work, its just a conceptual question)
Backbone is designed to be used in single-page applications, therefore it has a little to do with Rails routing. All Rails has to do is land a .html page, and everything else is client's concern.
If you intend to use Rails as an API, then its routing does not matter at all. You can even keep Rails application and frontend on different servers. In this case all what Rails has to do is process requests from the client.
For example, you can build your client-side without any hard ties to a backend, they can be absolutely separated. You just make AJAX calls from a client, then server processes them and responds with JSON.
Also, Backbone does not "route the javascript files to that html pages". It just executes functions according to a hash "route: action". These are just plain JavaScript functions which already can "see" JavaScript working within the document.

Resource Generators in Sinatra

I have developed few apps in Rails, and I needed to develop an API. I received the advise to build it in Sinatra, so I started looking into it.
It seemed quite nice, but it seems that a lot of things you get automated in Rails does not seem to exist in Sinatra. Specifically, I seem to have to write my resources from scratch. eg. The model itself, the migrations, and the REST routes.
I was wondering if there are any generators for Sinatra like the ones provided by Rails? Or should I simply use Rails if I want these kind of things automated?
Check out the sinatra-rest gem that can be used to handle RESTful routes. Quoted below for convenience:
[sinatra-rest is] a set of templates to introduce RESTful routes into Sinatra. The only thing for you to do is to provide the views. Automatically works nicely for models based on ActiveRecord, DataMapper, or Stone.
For example, if your model’s class is called Person you only need to add this line:
rest Person
Which will add the following RESTful routes to your application. (Note the pluralization of Person to the /people/* routes.)
Verb Route Controller View
GET /people index /people/index.haml
GET /people/new new /people/new.haml
POST /people create → redirect to show
GET /people/1 show /people/show.haml
GET /people/1/edit edit /people/edit.haml
PUT /people/1 update → redirect to show
DELETE /people/1 destroy → redirect to index
I don't personally use Sinatra but a lot of feedback I've gotten from other Rails developers is that they eventually end up switching back to Rails. I'm sure there are good arguments for using Sinatra over Rails, but if you already know Rails, and you don't have speed or application size constraints, I would just stick with that.
Another alternative to Sinatra is the Rails API project which doesn't include any of the view-related part of the Rails framework. I have used that in the past and liked it, but was it better than just using Rails? It's hard to say.
You should check out Padrino if you must have Sinatra.
Padrino is a ruby framework built upon the Sinatra web library.
Sinatra is a DSL for creating simple web applications in Ruby. Padrino
was created to make it fun and easy to code more advanced web
applications while still adhering to the spirit that makes Sinatra
great!
Or as Beerlington mentioned, you could use Rails API if you feel more at home with Rails. We've been using it recently with good success. We created a Simple API, with a mongo backend. Starts up very quickly :)
Or should I simply use Rails if I want these kind of things automated?
If you're that used to Rails that using Ruby is a problem, then maybe. Alternatively, you could try this API generator that uses Sinatra:
https://github.com/mattetti/Weasel-Diesel

Best practice for rendering data in the Backbone view templates

I was using backbone standalone for some time but currently I am trying to integrate it with Rails. Until now I used underscore templates and the question would be if it is possible to use Rails view helpers inside the template and if it is smart thing to do at all?
Update: Here is a simple example what I am talking about.
I have a list of messages and I have a MessageView for each message, I want to render the avatar thumbnail of the message author, link to his profile and description when the message was posted. Also I use markdown for the message content. With underscore templates I don't have access to the helpers to achieve this so I am forced to create methods on the model itself which feels really wrong...
You should take a look at the EJS Embedded JavaScript Framework, which provides rails-like standard view helpers like link_to, url_for, and other form tags.
Of course, you will have to translate your custom rails templates in js, but it's a start !
I ran into the same problem where I wanted to reuse my templates between Backbone and Rails. I ran into stache before: https://github.com/agoragames/stache
You can read more about the setup here: http://slainer68.wordpress.com/2011/09/20/partial-reuse-between-rails-js-the-easy-way/
Right out of the box, your underscore templates are pure javascript, so, in that sense, you can't really embed rails helpers into them. You can, however, make those templates ejb's (or whatever templating system you use) and have rails render them. With so little information, it's impossible to figure out what your app does, but it does feel weird to me to do that. I think, typically, your javascript templates are used for rendering html on the host side after some js functionality. Maybe a better description of what you are trying to accomplish?
Update ...
So you have some set of relationships between messages and authors in your rails models correct? You'd do a similar thing in your backbone models. So, you've got a User model, and a Message model. User has_many Messages, and Message has_one User. You can model that out in backbone as well... see my answer here:
Backbone set collection attribute (for the url)
You just need to describe the relationship on the backbone side.

Latest and accepted ajax techniques in Rails

What is the present state of Rails Ajax?
What frameworks and technologies should one use when working with Rails now?
Rails seems to evolve so rapidly that one might not be able to keep up.
Is it prototype and RJS or something else?
The point of RJS is that you don't really need to keep up with a framework, that the javascript functions are abstracted into Ruby for you to use.
That said, prototype is still the default choice, but there are plugins (http://ennerchi.com/projects/jrails) to implement RJS functions in jQuery. Of course, you could forgo the abstractions entirely and write the javascript however you'd like.
Personally, I find myself trying to stick to the Rails' default methods, and if I need a method that goes outside what RJS provides, making sure I am implementing it properly (ie. not duplicating what someone else has done), and usually doing it in prototype as to not have to load multiple javascript frameworks.
As said before, Rails' default javascript library is (and will probably always be) Prototype/Scriptaculous. However, when Rails 3.0 is released sometime around the beginning of May, it should be more accepting of other libraries such as jQuery.
If you don't like RJS, you can use a .js.erb extension and write javascript that will be sent through the erb template engine. This is my preferred way of doing things these days. You can see an example of this, and jQuery in this episode of Railscasts.

Resources