Best practice for rendering data in the Backbone view templates - ruby-on-rails

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.

Related

Site move to Rails - partials

I'm moving my site to Rails currently.
Beforehand I used jQuery load() method to insert a separate site html element. Now moving to Rails, I have to change it. From what I've learnt so far, the best way here is to use a Rails partial for this purpose.
Is this correct?
I would recommend taking a read through the Ruby on Rails Guides which are extremely helpful if you are just getting started in Rails. The specific section that might help is their documentation on using partials.
Yes, you are correct. Partials come in handy when you want to load some HTML elements conditionally, or want to reuse them in different places. A prime example for that is the footer or header of a website. Instead of writing the code in all the required files, simply render the partial wherever required. For further reading/implementation guide, you can refer the section on partials in the RoR guide

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.

I want to make an ajax driven static site in RoR

My goal is to create a Rails based site that uses AJAX to fetch different sections. The site should never completely refresh. I'm using Rails 3.2.8.
There's a lot of conflicting articles online about how to actually implement this. It seems to me that simply fetching pages.json and using javascript would accomplish my goal, but is that the "rails" way?
Every page that is users will see is static. I'll be using Rails as an admin to CRUD them, but that's it, and that portion doesn't need to be AJAX.
Take a look at backbone.js. For an ajax heavy site, that's exactly what you need to help organize your code and keep your front end consistent with your database. Also, check out this excellent railscast on implementing backbone in a rails project.
I noticed that you said static site. Well, if the site is completely static, why bother with something like rails? I would suggest just coding it in html and javascript because rails is intended to be used for dynamic, database driven sites.

How do I use rails helper in an emberjs application

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

Can I use symfony admin generator to manage two related modele at once

I'm in the process of learnign symfony and now I'm playing with the admin generator.
I'm doing a blog as test project.
I've managed to list my post & my comments but I was wondering if there was any way to have the comments crud below the post view instead of having to seperate pages ?
Thanks for reading
It should be noted that the template system that is available in the "Frontend", is also available in the admin generator. If you create /template files in your admin generator modules, whatever template file that is placed there will override the generated ones.
This being said, you can create highly customized interfaces in the admin generator using this method. Just override what you need, and let the admin generator handle the rest.
More information on how to use templates in the backend here:
http://www.symfony-project.org/jobeet/1_4/Doctrine/en/12#chapter_12_templates_customization
You are going to have to do custom code for this. As soon as you start stepping away from what the admin generator gives you out of the box it is usually simpler to write your own code rather than trying to extend the admin generator code (unless it is really simple)
By using a custom form & the admin generator, you can generate some rather complicated admin pages. You can embed an entire subform for a related object, have it display the actual choices for one to many and many to many relationships, and lots more. Essentially, if you create a form, it will generate a page from it.
While you could probably figure out a way to embed a subform for each comment, I don't think that would be a good idea. Generally, you only want to embed one form within another for a one to one relation, such as a user_details form embedded in the user form.

Resources