Note that there are some explanatory texts on larger screens.

plurals
  1. POwriting a meta refresh method for rails
    primarykey
    data
    text
    <p>I want a method in app/controllers/application.rb that can prepend/append text to whatever template gets rendered. Of course I can't call render twice w/o getting a double render error, so is this possible?</p> <p>I want to redirect after a delay using a meta refresh. Here's what I've got:</p> <p><strong>app/controllers/application_controller.rb:</strong></p> <pre><code>def redirect_after_delay (url, delay) @redirect_delay = delay @redirect_url = url render end </code></pre> <p><strong>app/views/layouts/application.html.erb</strong></p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;%= yield :refresh_tag %&gt; &lt;/head&gt; &lt;body&gt; &lt;%= yield %&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>So then if I want to add a redirect-after-delay, I add the following to 1) my controller and 2) the action's view:<br> <strong>app/controllers/my_controller.rb</strong></p> <pre><code>def my_action redirect_after_delay 'http://www.google.com', 3 if some_condition end </code></pre> <p><strong>app/views/my_controller/my_action.html.erb</strong></p> <pre><code>&lt;% content_for :refresh_tag do %&gt; &lt;meta http-equiv='refresh' content='&lt;%=@redirect_delay%&gt;;url=&lt;%=@redirect_url%&gt;'&gt; &lt;% end %&gt; &lt;h1&gt;Please wait while you are redirected...&lt;/h1&gt; </code></pre> <p>Since the <code>content_for</code> block never changes, is it possible to do this in some generic way so that I don't have to put <code>&lt;%= yield :refresh_tag %&gt;</code> in each template? (e.g. could <code>redirect_after_delay</code> add it into whatever template is going to be rendered?)</p>
    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.
 

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