Rendering Pagedown Markdown in Rails 4 - ruby-on-rails

I have a properly working pagedown editor in a rails 4 application, but I believe I'm missing something simple to render the pagedown data at a later point in my application.
Here's the gem (and initialization) I'm using: https://github.com/hughevans/pagedown-bootstrap-rails
Any ideas on how to render that data with the already-used gems?
EDIT: I think the root of my problem is it's not storing the data as the HTML version, so it doesn't render it when I display the data again. It's missing the converter step when the form gets saved, but I don't see any specific instruction on how to do that, so I assumed it was default part of these gems.
EDIT2: I've since taken the approach to convert the Markdown on each page load, with the following code:
Unfortunately, it doesn't use all the Pagedown Markdown, but it's at least handling new lines properly:
Any ideas?
Thanks!

So the answer to this question is two fold. Either you can convert the MD to HTML and store it in the database, or leave it as MD in the DB and convert it to HTML every time you want to render it. Note that you'll need to convert it back to MD (I'm not sure if this is entirely easy or not) if you want that field to be editable in the original MD.
Since this app doesn't care about performance, I decided to store it as MD and render it.
The results I was getting above stemmed from HAML's whitespace rendering that it does, so I had to use a little HAML filters to work around that.
The HAML ended up looking like:
.wmd-output><
:preserve
#{#object.attribute}
The second challenge was actually pretty straightforward, just not explicitly stated anywhere in the Markdown documentation. So I just wrote some javascript that automatically converts any .wmd-output class into it's proper Markdown on page load:
$(function() {
$('.wmd-output').each(function(i) {
var converter = new Markdown.Converter();
var content = $(this).html();
$(this).html(converter.makeHtml(content));
});
});
I hope this helps other people in the future.

This line of haml referenced should be what you need to render it:
= f.input :description, :as => :pagedown, :input_html => { :preview => true }

Related

How to allow users to edit dynamic slim page templates in production for rails 4?

Essentially I'm trying to implement a way so that users can edit slim that is stored in the database.
For example they would use the form to create a new page and insert the html for that page in a text field which would be saved in the database. I want to allow them to edit that page in slim. By the way the html stored is slim not plain html.
If I store slim in the database how do I get rails to render the html properly on the client side in production? So in other words would rails automatically do this since the view is being render like so:
views/page/view.html.slim
page.header
page.content
page.footer
or would I have to figure out a way to convert on the fly? I might be making this more complicated then I should but I'm new to this
If I understand you correctly you want to convert the slim to Html and output that in your views.
This is directly from slims doc. This is how it processes slim files and outputs it.
Tilt.new['template.slim'].render(scope)
Slim::Template.new('template.slim', optional_option_hash).render(scope)
Slim::Template.new(optional_option_hash) { source }.render(scope)
so in short
Slim::Template.new(page/view.html.slim).render
put that in a module to make it prettier and I think you're good. You may want to use rails path helper to get the direct link for the view. You may also want to consider figuring out a way to catch the errors in indentation so that your output doesn't bug out in production. Some kind of validation that prevents it from saving if not properly formatted should help.

Rails validation

I am suffering a issue with rails server side validation. Can some one help me out from this?
situation is :
I am creating dynamic for and its elements also dynamic.My application will generate the some HTMl code. Which can we use in any form or blog..
I applying the server side validation. But due to dynamic elements .I am not able to store the last entered value in to the elements. AS we normally does in PHP if user input something wrong we don't put the field empty. So I need to find a mechanism which fills the older values into the elements,If something went wrong.
This is the code into controller which is I'm using to show the form :
render :layout => false,:template=>'buildders/rander_form'
and view of rander_form.html.erb has
<%= render :file=>RAILS_ROOT+'/public/forms/form_'+#form_name+'.html.erb' %>
where #form_name is a dynamic form name(which have HTML code).
Can some one help me?
don't put erb files in public, people can download them by entering the file path in the url
also why not move that code out of the erb template into the controller?

rails 3 is printing html tags to screen instead of rendering them

I'm using vhochstein's fork of active_scaffold, which runs quite nicely on rails 3, except for a few small bugs - http://github.com/vhochstein/active_scaffold.
In rails 2.3, the following code disables a link:
return "<a class='disabled'>#{text}</a>" unless authorized
But in Rails 3, it causes the escaped html tags to be printed out instead as in the following photo:
How can I make the content of this return statement render the way it should in rails 3?
The code above, is from the list_column_helpers.rb file in vendor/plugins/active_scaffold/helpers/
UPDATE:
Floatless fixed this by suggesting to add .html_safe to the code.
I have since found that the folowing change also needs to be made as there's more than one bit of code that is respondible for disabling action links in active_Scaffold:
In /plugins/active_scaffold/frontends/default/views/_list_actions.html.erb change:
<%= record.authorized_for?(:crud_type => etc etc etc -%>
By making it use "raw"
i.e.
<%= raw record.authorized_for?(:crud_type => etc etc etc -%>
Anyway, thanks to floatless and hopefully mr hochstein will be able to use this stuff.
Try this:
return "<a class='disabled'>#{text}</a>".html_safe unless authorized

Markdown to text/plain and text/html for multipart email

I’m looking for a solution to send DRY multipart emails in Rails. With DRY I mean that the content for the mail is only defined once.
I’ve thought about some possible solutions but haven’t found any existing implementations.
The solutions I’ve thought about are:
load the text from I18n and apply Markdown for the html mail and apply Markdown with a special output type for the text mail where
links are put in parenthesis after the link text
bold, italic and other formatting that doesn't make sense are removed
ordered and unordered lists are maintained
generate only the html mail and convert that to text according to the above conditions
Is there any available solution out there? Which one is probably the better way to do it?
In Chapter 4 of Crafting Rails Applications, Jóse Valim walks you through how to make a "merb" handler that uses markdown with interspersed erb and can compile to text and html. Then you make a mailer generator that generates a single merb template for each of your mail actions.
You can read an excerpt from that chapter on the page I linked you to. I highly recommend buying the book.
If you're interested in using my sorry version of what he describes in that book, you can slap this in your Gemfile:
gem 'handlers', :git => "git://github.com/chadoh/handlers.git"
Be warned that I barely know what I'm doing, that I'm not versioning that gem, and that I probably won't really even maintain it. Frankly, I wish I could find someone else who was doing a better job, but I've been unsuccessful in doing so. If you want to fork my project and be the person doing that better job, go for it!
This is a PITA, but is the only way to DRY mail such that you can support both HTML (multipart) & plaintext:
Put the html email copy in a partial file in your ActionMailer view directory with the following extension: _action.html.erb
Replace "action" with whatever action name you are using.
Then create 2 more files in the same directory:
action.text.html.erb and
action.text.plain.erb
In the text.html partial:
<%= render "action.html", :locals => {:html => true} %>
In the text.plain partial:
<% content = render "action.html", :locals => {:html => false} %>
<%= strip_tags(content) %>
That works for me, though it certainly makes me want to pay the monthly service for madmimi
Use the maildown gem.
This gems does the heavy lifting of allowing you to use email.md.erb instead of email.html.erb and email.text.erb. Write it once in a sane format and have it automatically display in HTML and in Plain Text. Win.
There are some intricacies here that you'll want to look at based on your use-case, but here's some of what we did to get it working well:
Create a maildown.rb initializer to setup some sane defaults:
Maildown.allow_indentation = true # Prevents code blocks from forming when using indentiation in markdown emails.
Maildown::MarkdownEngine.set_text do |text|
text.gsub( /{:.*}\n?/, "" ) # Removes Kramdown annotations that apply classes, etc. with `{: .class }`.
This allows you to use indents in your blocks, etc. But also precludes the ability to add indents in your Plain Text. It also removes Kramdown-specific annotation from Plain Text.
Then just replace your HTML and Plain Text files with a single .md.erb file and test it out to make sure it looks good in both versions.
Note, until you remove the .html.erb and .text.erb files, it will show those first before looking for a .md.erb file. This may actually be a nice feature if you ever needed to write separate formats for a specific email (maybe a marketing one that requires more complex formatting than Markdown can provide) without having to specify anything anywhere.
Works a treat.

Parsing a 'template' in rails (alternatives to gsub)

I have a 'template' system in my CMS rails app. Basically the whole HTML template is stored in a database column and it has key code in the string (like <!--THEME_Body-->) that gets replaced with content generated by the application.
I use an actual layout to render the template. All that is in the layout is:
<%= generate_theme.gsub!('<!--THEME_Body-->', yield) -%>
That helper takes the correct theme and further gsubs other area like Meta data and Breadcrumbs.
I'm just wondering if there's a better way to go about this? Perhaps using content_for or something like that?
That's a pretty good way of going about it, although I would make use of Ruby's sexy syntax to combine all the lines into something a little more syntactically correct - it speeds the script up and it looks a damn sight nicer too!
tags = {
'<!--THEME_Body-->' => yield,
'<!--THEME_Head-->' => yield(:head),
'<!--STYLESHEET-->' => stylesheet_link_tag('application')
}
tags.each { |str, rep| generate_theme.gsub!(str, rep) }
Bear in mind that this code should not go in a view - it should ideally be put in a model, as it's to do with the application's data, but it could also go in a helper somewhere. If it's an instance variable in a model, you could just call
generate_theme.parse
And that code could be executed - it looks a lot better and sticks to the standard MVC convention of cleaning up the view as much as possible.
Jamie

Resources