Using Rails helper in Angular template - ruby-on-rails

I am using Rails with AngularJS and I would like to use rails helper and erb-style if statement in an Angular template.
I am displaying a delete button and want to make sure only the owner can see this button.
In a normal erb file, I would write:
<% if #article.id == current_user.id %>
<button>Delete</button>
<% end %>
I attempted to add erb extension to my template files, but I could not use embedded ruby syntax, nor the helper current_user.
Another approach I took was adding ng-show='owner' to the button, and define owner function in the Angular controller to determine if the current user is the owner. But this did not work as I don't have access to current_user.
Any suggestions? If there is a more Angular way to do this, I would also like to know.

I think one of the hardest things with AngularJS is switching to a mindset of "data retrieved from the server" not "data available directly in the server".
That is, you need to abandon the idea that you can use any rails-like conventions in your template (same goes for Django or PHP or any other framework).
Instead, think of how you can add an API to your Rails code that returns the data in question in a clean JSON format. Then write an angular service that calls it, say ArticleService.get(id), and then your angular template will be straightforward.

Related

Rails PDF generation in model

I am writing and invoicing application in Ruby On Rails, which is supposed to generate a PDF from a model so called out_invoice to an in_invoice under certain conditions.
The PDF for the out_invoice is already separately generated on demand using wicked_pdf gem basically having a HTML template and sending back the PDF inside the action and downloading it.
I have successfully programmed the duplication inside the out_invoice model which works fine for all values of the out_invoice. Including an attached PDF in a separate attribute.
But instead of generating the PDF and attaching it to the in_invoice. It fails. Some Controller/Model MVC thing I assume!
The PDF for the out_invoice is currently generated separately in a controller action so I tried copying the code: I tried rendering the template inside the model which seems to work fine, but "sending" is not defined for Rails models. If I just return the WickedPDF without sending it I get an error: ActiveSupport::MessageVerifier::InvalidSignature
Must be something super simple I am forgetting + I have no clue what is the reason for the error. I am not a front-end guy so don't want to use something else as the already existing HTML template for rendering. But worst case I write the template newly in another engine. Looked at Prawn but this means rewriting everything.
Can somebody help? Thanks!

Find url for webpage based on a partial file

I was introduced to a large team and not very familiar with the site I am developing for. I need to make an edit to a partial (i.e. _partial_file.html.haml) and need to verify the change visually but I don't know where that partial is being displayed on the website. Is there a way I can get a url from a partial file with rails.
For example, could I run a rake routes | grep or do something in the rails console to help find any URL where the partial is displayed?
Thanks
If I understand you correctly. While in your rails app root.
grep -r "partial_file" .
This will allow you to look for instances of partial_file and where it's being rendered. You might see it in a view or a controller. Depending on where you find it, you can rake routes and see just what url you should enter to see your output
When you render a page you should see output in the console for all templates being rendered.
example rendering template show.html.haml from SomeController#index
Use this to locate the template that is including your partial. You can then use that to search the entire project for all templates including that partial.
Rails renders templates by a specific convention that maps controller methods to specific layouts and templates. I would familiarize yourself with http://guides.rubyonrails.org/layouts_and_rendering.html to get a better idea of how the default internals work for you rails app and use that as a starting point.

Sharing templates beetween JS and Rails using Haml + Mustache

I am trying to make common templates that used both by Rails and the client side, ie in coffescript.
I use hogan_assets and haml_assets to export templates to JS. However I can't find a way to use HAML and Mustache to render server-side views.
Partial solution is described here, but it doesn't work with view helpers, ie, "render partial" doesn't work.
For some unknown reason chaining of handlers, such as .mustache.haml, doesn't work, and I can't find neither good info on Rails template handler nor an example on how to build "chainable" handler.
I've been experimenting with something like smt_rails lately, but I have yet to find a silver bullet.

Include FormTagHelper in model?

I am trying to take an html file created by MSWord (don't ask) clean it up and create a form out of it on the fly. I am doing this work in the model (clean it up, create the form, stuff it in a DB). I would like to use the form_tag helpers, but can't quite get the hang of including them for use in the model. When I do an "include ActionView::Helpers::FormTagHelper" the helpers aren't available.
So, I figure either
I should be doing the clean up & create in a helper
I fundamentally don't grasp including in a model
All of the above.
Suggestions?
Form tags should not be included in the model. They are for views only.
I've got to ask. Why? It sounds like you're in for a wold of hurt.

Rails 3.0 beta & JS Helpers with jQuery

In the release notes for Rails 3.0 beta it says:
"Unobtrusive JavaScript helpers with drivers for Prototype, jQuery"
So how do I setup Rails 3 to use jQuery then? It still loads all the Prototype libraries by default.
I took this to mean that Rails 3 has built in functionality similar to the jRails plugin, but maybe I'm misunderstanding :)
Also, as a bonus question, if I am using Prototype is there a way to get Rails to load the minified versions, and even better a single concatenated JS file to cut down on http requests?
Thanks.
When you create a Rails 3 app, just pass along the -J param as well:
$ rails app_name -J
This will skip over including the Prototype libraries. Now, all you need to do is drop the latest jquery.js file into the public/javascripts directory. Once you do that, you'll also need the jQuery version of the rails.js file. You can get that here:
http://github.com/rails/jquery-ujs/blob/master/src/rails.js
EDIT: You need to include these files in the top of your layouts to gain the functionality. You can do this by:
<%= javascript_include_tag "jquery", "rails" %>
Hope this helps!
Unobtrusive JS doesn't refer to the PrototypeHelper methods, but to remote forms and links and the like. The concept is that you include :remote => true in your form_for or whatever helper methods support it, and then a driver called rails.js will look for those remotes and intercept the submit or click events and send them via xhr.
To use jquery you'll just need to replace the prototype ujs driver (which ships with rails) with the jquery ujs driver, that was extracted into its own repo shortly before the rails 3 beta release. You can find it here.
Check out Google Closure
It can turn multiple javascript files into a single compressed js file. It can even figure out which parts of the library you aren't using and remove them as well.
I don't know about Rails 3, but I'll try to answer bonus question.
You can put whatever you want in public/javascript directory. By default it will load files: prototype.js, effects.js, dragdrop.js and controls.js (read more). If you want to compress all js files and send it in one file, you can use this plugin.
Of course it won't work for dynamicaly generated js files.
I'm also working on that with trying to convert my old ajax with rails 3.
From what I can tell, they moved to a structure that adds a data-remote=true and when you add :remote => true to something like link_to which is supposed to replace link_to_remote in rails 3, so there are no more onclick methods that calls Ajax methods.
So, how does Ajax work in Rails 3, then? Well, you are supposed to have javascript methods that watch for when you click links that have a property of data-remote=true and does something with it, but don't actually include them in Rails (from what I can tell), it's library agnostic since you can write these methods to watch for clicks in prototype, jquery, write them yourself, or whatever else is out there.
I did find some javascript on github to get started that will watch for these events:
In prototype
In jQuery
I think in order to actually load jquery instead of prototype, you're going to have to just download it to public/javascripts and manually specify jquery, use javascript_include_tag :all, or override javascript_include_tag (not recommended)

Resources