Using mustache templates as partial views in Rails 4? - ruby-on-rails

I'm trying to use mustache views in Rails 4 instead of some old partial views that hosted some HTML templates I am sharing with a javascript application.
Added config/initializers/mustache.rb
# Tell Rails how to render mustache templates
module MustacheTemplateHandler
def self.call(template)
#assigns contains all the instance_variables defined on the controller's view method
"Mustache.render(#{template.source.inspect}, assigns).html_safe"
end
end
# Register a mustache handler in the Rails template engine
ActionView::Template.register_template_handler(:mustache, MustacheTemplateHandler)
I named my template _template.mustache and put it the template in views/sales/_template.mustache and I can render it just fine from normal .html.erb with render partial: 'template'
Where do I put the Template.rb file for it to work with the mustache?
class Template < Mustache
def something
# return something
"WOW"
end
end

So, you can write this variables directly to yours controller.

Related

How to render a file which is in an included engine in Rails 4.2?

In my Rails 4.2 app which consists of a few Rails engines, there is a need for engine itemlized_spendingx to render a file in engine biz_workflowx with ajax call. The engine biz_workflowx is included in itemlized_spendingx's application controller like this :
module ItemlizedSpendingx
class ApplicationController < ::ApplicationController
include BizWorkflowx::WfHelper
end
end
In BizWorkflowx::WfHelper, there is a action event_action defined and it is called ajax with link_to from index view in engine itemlized_spendingx. Here is the event_action.js.erb responding to ajax call
$("#newworkflow .modal-content").html('<%= j render(:file => "biz_workflowx/application/event_action.html.erb") %>');
$("#newworkflow").modal();
The purpose of the evant_action.js.erb above is to popup a view of event_action.html.erb (both event_action.js.erb and event_action.html.erb reside in same subdir under biz_workflowx/app/views/application/) with Bootstrap modal. The problem I am having is that render(:file => ..) raises error of template missing even though the File.file?('biz_workflows/app/views/application/event_action.html.erb) returns true in ActionView resolver.
It seems that the render is really having hard time to locate event_action.html.erb even though File.file? sees it. How to make the renderer see it?
Here is the event_action.js.erb that works:
$("#newworkflow .modal-content").html('<%= j render(:file => "/application/event_action.html.erb") %>');
$("#newworkflow").modal();
After removing the biz_workflowx, then the view template is found. It seems that the Rails view resolves the view template in a relative path and treats event_action.html.erb under the scope of itemlized_spendingx.

ruby code is showing as html in template

I have a rails app which uses angularjs. Here I'm trying to render a template from rails controller and pass a resource to the template. But the ruby code for displaying the variables are showing exactly as it is in the html.erb view.
def fail
#order = Order.find(1)
render 'payments/fail'
end
in view
<%= #order.as_json %>
My guess would be that the problem is in the name of your view file. I'd guess that you named it something like fail.html instead of fail.html.erb. Without the .erb suffix, Rails just interprets the file as html text and renders it without interpreting the ruby code.
However, changing the file name isn't quite the correct solution. Since you want to render json instead of HTML you don't need to create a view template, so you should just delete the template file altogether.
All Rails models have an .as_json method automatically, so you can simply modify your controller's fail method like so:
def fail
#order = Order.find(1)
render json: #order.as_json
end
Also if you want to do something fancy and modify the json that is returned, you can define your own as_json method inside the model.

Rebuild Haml assets in development

I'm working on an angular project and I'm using the angular-rails-templates gem to manage the templates. I have a custom method in rails in order to create 'partials' between my templates.
module HamlHelper
def include_partial(partial, locals = {})
Haml::Engine.new(File.read("#{Rails.root.to_s}/app/assets/javascripts/#{partial}")).render(Object.new, locals)
end
end
So that I can have in my haml templates for example:
/assets/javascripts/templates/page1.html.haml
- header = { title: 'Page 1'}
.page
= include_partial 'shared/header.html.haml', locals: header
...
and
/assets/javascripts/shared/header.html.haml
%header.header
.header__container
%h1.header__title= locals[:title]
...
The problem I'm having is that if I want to modify the header.html.haml partial, the changes are only updated when I modify the page1.html.haml file which contains the partial. So since page1 is not modified, Rails thinks that it wasn't modified and then page1 is not processed again. Is there a solution for this?
NOTE: I used here a very basic example for my partial, but in reality is a more complex one. So using ng-include of angular is not what I need.

Ruby on Rails - using helpers in html.erb

I'm probably missing some things.
Say for example I have a helper function in app/helpers/foo_controller.rb and the code is as follows:
def sample_helper(count)
#implementaton...
end
and I want to use this helper in a webpage generated by rails and the code is as follows:
<%= sample_helper(user.id) %>
and if I try to run the webpage it will throw me an error saying that the method is not defined.
Thanks in advance!
You don't quite have the naming conventions right.
Name your helper file app/helpers/foo_helper.rb and in it you should have this:
module FooHelper
def sample_helper(count)
"#{count} items" # or whatever
end
end
And now, from any view rendered by FooController you should be able to use the sample_helper method.
Also, you should know that if you use the rails generators this structure is setup for you. All you need to do is add methods to the files that get generated. That way you don't need to guess the naming conventions.
For example, this command will make a controller file, controller test files, a helper file, and and an index view file, all ready for you to customize.
rails g controller foo index
Is your helper should be in a file called app/helpers/foo_helper.rb that contains a a module of the same name as the helper (camelized) Like:
module FooHelper
def sample_helper(cont)
# implementation
end
end
That's the way Rail auto loads helpers.

Ruby on Rails views names dependences

May be my question it's a little weird for RoR developers, but i'm new in Ruby on Rails, and i only discover this world now - there is some dependencies in views names and definitions in controller?
If i have, for example, view called "parse-public-profile.html.erb" , should i add in controller definition with exactly this name? i mean "def parse-public-profile ... end"
I know, that this is basic, but simply i try to understand how controller knows, what views i have now; what i should change, if i will add/change-name of view, or how to define view, if in my "views" folder, i have another folder, for ex. "clients"
Thanks!
Rails follows REST this means methods as index, show, edit, update, destroy etc. are very common in an Rails controller. When you have a custom action(method) however on your controller Rails will look for the corresponding view file, so for example:
class UsersController < ApplicationController
def another_action
end
end
will try to render: app/views/users/another_action.html.erb
There is also the concept of partials which are normally called within a view file f.e. in users/index.html.erb
<% render :partial => 'form' %>
will try to render: app/views/users/_form.html.erb (note the _)
An in depth explanation can be found in the Rails guides
You can also use:
def index
render :template => "users/parse-public-profile"
end
The :template over rides the default file that Rails would have rendered.
For more info, see the Rails Guide on Layouts and Rendering at http://guides.rubyonrails.org/layouts_and_rendering.html.

Resources