Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After having a look at <a href="https://stackoverflow.com/a/4901393/567863">this StackOverflow answer</a>, it seemed that the best location for i18n Markdown files would be their own action name directories under the <strong>config/locales</strong> directory, and that there was a good opportunity to refactor the <code>render</code> code on all views for the <code>StaticPagesController</code>. So, using <strong>about.html.haml</strong> as an example below, the call to <code>render</code> in the <strong>home</strong>, <strong>help</strong>, <strong>about</strong>, and <strong>contact</strong> views has been changed to the exact same code:</p> <p><strong>app/views/static_pages/about.html.haml</strong></p> <pre><code>- provide(:title, t('.about_us')) :markdown #{render file: localized_page_for(action_name, params[:locale])} </code></pre> <p>The <code>localized_page_for</code> method is defined in the <code>StaticPagesHelper</code>:</p> <p><strong>app/helpers/static_pages_helper.rb</strong></p> <pre><code>module StaticPagesHelper def localized_page_for(action, locale) "#{Rails.root}/config/locales/#{action}/#{action}.#{locale.to_s}.md" end end </code></pre> <p>So, now all the Markdown files have been taken out of the <strong>app/views/static_pages</strong> directory and are called from their respective logical directories (eg. <strong>config/locales/about/about.en.md</strong> etc) using <code>ActionController</code>'s <code>action_name</code> attribute and the locale, making for less clutter.</p> <p>As for question 2 above, string-interpolation seems to be common enough for this kind of problem, so I'll consider it "elegant" enough as well.</p> <p>As for question 3 above, after exhaustive searching, I haven't found a way anyone has passed in variables in to a pure Markdown file, and <a href="http://daringfireball.net/projects/markdown/syntax" rel="nofollow noreferrer">the documentation</a> doesn't seem to say anything about supporting them, so I'm going to conclude that it's not possible. If passing Ruby variables in to Markdown is absolutely necessary, the file will need to be run through another interpreter, kind of like is described in <a href="https://stackoverflow.com/a/10061808/567863">this StackOverflow answer</a>.</p> <h3><strong>Update:</strong></h3> <p>After running security scanner <a href="http://brakemanscanner.org/" rel="nofollow noreferrer">Brakeman</a> against the app, it came up with a potential <a href="http://brakemanscanner.org/docs/warning_types/dynamic_render_path/" rel="nofollow noreferrer">Dynamic Render Path</a> security warning (albeit a weak one) due to dynamically passing in <code>params[:locale]</code> to the <code>render</code> call instead of passing it a static string. So, I moved the call to the <code>localized_page</code> method out of the views, moved the method itself out of the <code>StaticPagesHelper</code> (so that file is now empty) and in to the <code>StaticPagesController</code> and then instantiated a <code>@page</code> instance variable in each method to pass to the view. In summary, the code now looks like this, which doesn't get the security warning:</p> <p><strong>app/controllers/static_pages_controller.rb</strong></p> <pre><code>class StaticPagesController &lt; ApplicationController before_filter :localized_page, only: [:help, :about, :contact] def home if signed_in? @micropost = current_user.microposts.build @feed_items = current_user.feed.paginate(page: params[:page]) else localized_page end end def help end def about end def contact end private def localized_page @page = "#{Rails.root}/config/locales/"\ "#{action_name}/#{action_name}.#{params[:locale].to_s}.md" end end </code></pre> <p><strong>app/views/static_pages/about.html.haml</strong></p> <pre><code>- provide(:title, t('.about_us')) :markdown #{render file: @page} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload