Convert html to rails helpers - ruby-on-rails

I am a RoR developer. I work with a graphic designer who prepares static html/css files and forward them to me.
Then I covert those html code to haml and include them in my views.
I use http://html2haml.heroku.com/ to covert html to haml. But it doesn't covert to haml using Rails helpers.
Example:
It converts
<img src="abc.png" />
To
%img{:src => "abc.png"}/
But I need it to be converted to
=image_tag "abc.png"
The same for other Rails helpers (link_to, text_field, ...etc)
Is there anything can make my work easier and converts using helpers?

I think, there's only one solution to this: your views should be built with Rails helpers from its beginning. There's many tools that can help create static views (which become your project views then), such as Serve or Middleman. The downside is that your designer should know about Rails helpers, haml etc, but this is a classical tradeoff.

Related

Configure ckeditor in rails to write in Markdown syntax as opposed to HTML

In one part of my application I am successfully using creditor. It properly takes the text typed in and converts it into html format.
User Types in the content:
And it properly saves the content in html syntax:
In another place in my application I want to again use ckeditor. However: this time I want the content to be converted into Markdown syntax as opposed to HTML syntax. Is this possible? If so, how do you specify that configuration for this one place in the application (as opposed to globally specifying this rule because everywhere else I want ckeditor to continue converting the content into html).
I did look within the ruby gems ckeditor docs. I also looked through the ckeditor documentation on options, however looking through that was a bit overwhelming to me.
Seems like you have to add the markdown addon as it is not part of ckeditor, then follow the documentation.
What I ended up doing was creating a script that converted the database fields in my application that were in markdown syntax into html syntax. I did this with the redcarpet Gem and the rdiscount Gem. With those fields now saved in html syntax instead of markdown syntax, I rendered the text just like I do everywhere else where ckeditor is used.

html.haml to html.erb converter for Rails

I inherited a bunch of html.haml views in Rails project and want them to be html.erb.
Could you suggest some automatic Rails-aware converter?
Everything I've found is a set of HTML to HAML converters that additionally do not understand Rails.
You can use this converter to convert manually, link below
https://haml2erb.org/
I don't think any automatic html to erb converter is available as of now!

How can I find helper methods to create html tags outside of views in rails

Im creating a seeds file, generating dummy posts with dummy html. Im using Faker gem to help with ipsum text but I also want to be able to add html tags (img tags in particular) without the need to hard code. Is there any helpers like those in actionview ie image_tag.
Here you go:
ActionController::Base.helpers.image_tag("image.png")
You can access all helpers from ActionController::Base.helpers
ActionController::Base.helpers.image_tag("foo")

How to register rb as a template handler in rails

Well since I am using a lot of helper methods in my view files and I avoid using html in most of my view files.
Example
myview.html.erb
<%=myhelper #myobject%>
so I end up using,the erb processing tags each time for each file.
<%=%>
I want to register .rb as a template handler or any other extention for that matter.
So my templates look like
myview.html.rb
myhelper #myobject
I am clueless on how to go ahead.
I found it,it seems railscasts already covered that part.
Its show notes,worth checking out.
https://github.com/railscasts/379-template-handlers/blob/master/store-after/config/initializers/ruby_template_handler.rb
Things without ruby are easy to read and render without those erb tags.

Where in the Rails framework should I place my Backbone templates?

I'm a rails developer trying to learn Backbone and then I ran into this problem: since Underscore templates include symbols like <%=%>, I guess templates can't be included into erb files, so is it okay to have a rails partial for every single template? And what extension should it be?
You can escape the erb symbols by using two % in the opening tag, and put your backbone templates in the rails views:
<script type='text/template' id="my-template'>
<%%= name %>
</script>
will output the following in your page:
<script type='text/template' id="my-template'>
<%= name %>
</script>
Putting your Backbone templates directly in your rails views is IMHO the best option when you're trying to learn. You're already wrestling with the new concepts, no need to add another hurdle.
Starting with Rails 3.1, it provides two things that make working with Backbone templates a little easier: the asset pipeline, and automatic JST (JavaScript Template) compilation.
Create a directory in your app/assets folder called templates. This directory will automatically be picked up by the asset pipeline.
Next, name the files in that directory with an extension of jst and the type of template you are creating ejs (embedded javascript). You can even nest them in directories. For example:
app/assets/templates/my_template.jst.ejs
app/assets/templates/bookmarks/show.jst.ejs
The asset pipeline also allows you to use other templating languages like embedded coffeescript, mustache, handlebars, etc. by simply changing the file extension (and including any necessary gems).
Now to reference your JST templates in your Backbone views, simply use the path to the filename:
var Bookmark = Backbone.View.extend({
template: JST['bookmarks/show'],
render: function() {
this.$el.html(this.template(this.model.attributes));
return this;
}
});
You may need to add this line to your application.js:
// require_tree ../templates
Here's a nice article which explains all of this in a little more detail: http://www.bigjason.com/blog/precompiled-javascript-templates-rails-3-1
Where should you put your Backbone templates? I'd say nowhere. I believe that in most Rails applications, the server should be responsible for all rendering of HTML, while the client-side JavaScript should just be responsible for inserting that rendered HTML into the DOM. Among other things, this makes I18n easier.
The exception would be if Rails is simply being used as a lightweight backend for an application that runs mostly on the client side (though in that case, you might want to use Sinatra or something instead). In this case, Rails should probably render nothing, and have the JS do all the rendering.
Notice the underlying principle here. Either the server should be responsible for all rendering, or the client should. Splitting it will make life harder.

Resources