put a rails collection into asset templates - ruby-on-rails

I have a backbone.js front-end and Rails backend, and all my template files are in the assets directory.
I want to implement a select list in one of my templates, with the data coming from a controller.
For example, I have an AccountController and I want the user to be able to select from available accounts. Right now, I've got an AJAX request to build the accounts select list, but that isn't efficient.
Is there a way to inject my Rails data into my templates before the templates are sent to the user?

Assuming you are OK with using mustache.js to render your templates, poirot can do exactly what you're trying to do. With this you can have rails data rendered into the template before rendering the template on the front-end, and you can also use the same mustache template for both back-end and front-end. Keep it DRY yo.
Another way to solve the problem would be to name your JS files like so: example.js.erb, which allow you to render the select HTML or data directly to the JS file, and use it as needed.

Related

rubymine view html generated by erb rails

Is it possible to see the html generated by html.erb files within RubyMine? I know that I have been able to see the css.min generated by css, so there must be a similar option to be able to see the direct html generated by embedded ruby?
With your Rails server running you can right click a view template file in the project tree and select open in browser
But this is essentially the same as visiting localhost:3000/posts manually, e.g.
Rubymine can't render the view and make the necessary calls to the controller actions and database without the whole app running

use of partial views (file upload forms on different pages)

I am very new to asp.net MVC.
I need to create dnd file "component". It allows user to upload file to server using DnD, the upload is done via webAPI. I am using FileModel here, for example, webApi also returns FileModel (to show uploaded file info on page). I did this part.
However, I did it in the "TestView". I need ability to add this "component" to any view on my project. Unfourtunately I do not have knowledge enough to do this.
I have found out that there are PartialViews. Should I just move my "TestView" to some "FileUploadPartialView"? What problems this will cause? How this affect the usage of FileModel?
Partial Views are like a javascript include. You shouldn't have any trouble.
Just put this wherever you need to have your partial view...
#Html.Partial("_FileUploadPartialView")
Let me know if you need more detail.

Precompile Hogan.js Templating Server Side ASP.NET MVC

I'm taking a look at Hogan.js by Twitter.
http://twitter.github.com/hogan.js/
They talk about being able to precompile templates via the server which I understand can be a perf gain.
Currently every time I render the template I perform the following after an AJAX hit to the server to get data:
var template = Hogan.compile($('#seasonsTmpl').html());
$('#main').html(template.render(data));
Given the following template:
<script type="text/html" id="seasonsTmpl">
<ul>
{{#season}}
<li>{{.}}</li>
{{/season}}
</ul>
</script>
What can I do to "precompile" server side using an ASP.MVC backend? Is this not possible as it seems to be centered around using Node.js?
You have the right idea to optimize your templates. There are two options, and the choice probably depends on whether you want to render your templates client-side or server-side.
If you want to render them client-side, you can do a true precompile using Hogan.js. Yes, this does not run on .NET, but I think you've misunderstood when precompilation is possible. Rather than expecting it to happen on each web request, or page load, you can compile your templates up-front as part of your build process. You will need to install node and npm to set this up, but you only need to run this locally on your own machine, or a build box if you use one. Whenever you update your templates, you would run Hogan again to update the output file. The compiled output will be a JavaScript file full of functions that are optimized for later use. These functions include your template strings, along with the logic to render the data a la Mustache. You can then include the output file just like any other JavaScript include, or include it with the other sources for minification if you do that.
The second option is to render the templates server-side. This is different than precompilation, the server will compile and render the templates again for each web request. Step away from Hogan.js and look at a .NET alternative such as Nustache. The great thing about Mustache is it has a spec and has been ported to several server-side languages.
There's a fundamental difference in these options in terms of where the rendering happens. You might even want to leverage both approaches in certain scenarios, say if you want to render the initial page load server-side using Nustache, but have dynamic elements that must be rendered in the browser using templates precompiled through Hogan.
More info:
Nustache on Github
I hope you find this helpful!

Is it possible to render a view file from another website?

I am using Ruby on Rails 3 and I would like to know if it is possible to render a view file (a partial template) from another RoR application (anotherr website). If so, and if the view contains a form, it is possible to submit that form sending information over HTTPS?
Yes.
Here is how you might do it.
Create a symlink from the controller's view folder to the partial in the other application.
ln -s path_to_existing/_existingPartial.html.erb path_to_referring/_existingPartial.html.erb
You can now reference the existingPartial in your views as if it were in the same application.
The form will work as long as the target of the form posts to a valid URL in your current directory.
That said, this isn't the best way to do this. A couple other options:
Create a Rails plugin both apps use
Create a Gem both apps use
Depending on your source control software, you can reference the same file in both source trees (for example, using Subversion external references)
Rails supports rendering an arbitrary file
render "/old_app/current/app/views/pages/show"

Adding html form and input tags into Symfony static pages

I inherited the management of a Symfony site and need to add some HTMl form tags to one of the "static" pages via the CMS. The scenario I have is:
/index.php/splash/welcome pulls up the welcome screen.
We want to be able to add a subscription button on that page.
The HTML has been supplied for us by the company that handles the subscriptions.
The form post method has an action that references a script on a remote site (no lectures on the security implications please ;-).
When I add the <form... and <input... tags via the CMS admin panel, the tags get removed automatically by Symfony.
Is there a way to tell Symfony to allow these tags?
Thanks in advance,
Marty.
This is goign to depend completely on how the developer set up the CMS. Youre using a rich text editor in source mode i would take a look at that editor's config file and documentation because its probably the one responsible for stripping the tags.
If its just a plain text area i would check the submit action for the edit form and take a look at the code... he may be using something to strip them in there.
If youre using one of the Symfony CMS plugins (Diem, Apostrophe, Sympal) i might be able to help further if i know which one youre using. If its something custom we would need to see the code. This isnt really indiciative of the Symfony core, but rather the CMS youre using.

Resources