Rails 5: error passing time_ago_in_words from controller to view











up vote
0
down vote

favorite












I want to prevent users from deleting comments that are more than X minutes old. (This is determined by a status field.) I anticipate that the value of X may change in future so it's currently defined in an initializer:



AGE = 1.minute 


What's the best way to pass the error message from the Comment controller to the (Server-generated Javascript Response) view?



if @comment.status == "locked"
render "comments/too-old", locals: {message: "You can't delete this comment now as it's more than #{time_ago_in_words(AGE.ago)} old."}
end


This currently fails as we're not allowed to use time_ago_in_words in a controller:




undefined method `time_ago_in_words'











share|improve this question




























    up vote
    0
    down vote

    favorite












    I want to prevent users from deleting comments that are more than X minutes old. (This is determined by a status field.) I anticipate that the value of X may change in future so it's currently defined in an initializer:



    AGE = 1.minute 


    What's the best way to pass the error message from the Comment controller to the (Server-generated Javascript Response) view?



    if @comment.status == "locked"
    render "comments/too-old", locals: {message: "You can't delete this comment now as it's more than #{time_ago_in_words(AGE.ago)} old."}
    end


    This currently fails as we're not allowed to use time_ago_in_words in a controller:




    undefined method `time_ago_in_words'











    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to prevent users from deleting comments that are more than X minutes old. (This is determined by a status field.) I anticipate that the value of X may change in future so it's currently defined in an initializer:



      AGE = 1.minute 


      What's the best way to pass the error message from the Comment controller to the (Server-generated Javascript Response) view?



      if @comment.status == "locked"
      render "comments/too-old", locals: {message: "You can't delete this comment now as it's more than #{time_ago_in_words(AGE.ago)} old."}
      end


      This currently fails as we're not allowed to use time_ago_in_words in a controller:




      undefined method `time_ago_in_words'











      share|improve this question















      I want to prevent users from deleting comments that are more than X minutes old. (This is determined by a status field.) I anticipate that the value of X may change in future so it's currently defined in an initializer:



      AGE = 1.minute 


      What's the best way to pass the error message from the Comment controller to the (Server-generated Javascript Response) view?



      if @comment.status == "locked"
      render "comments/too-old", locals: {message: "You can't delete this comment now as it's more than #{time_ago_in_words(AGE.ago)} old."}
      end


      This currently fails as we're not allowed to use time_ago_in_words in a controller:




      undefined method `time_ago_in_words'








      ruby-on-rails






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 3:24

























      asked Nov 8 at 3:18









      MSC

      1,60321526




      1,60321526
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The method time_ago_in_words is from ActionView::Helpers::DateHelper helper which will be automatically included in views.



          If you need to access this method from controller then, you need to include this helper in the controller.



          Assuming the controller name is CommentsController and action name as destroy:



          include ActionView::Helpers::DateHelper

          class CommentsController < ApplicationController
          def destroy
          ...
          if @comment.status == "locked"
          render "comments/too-old", locals: {message: "You can't delete this comment now as it's more than #{time_ago_in_words(AGE.ago)} old."}
          end
          ...
          end
          end





          share|improve this answer





















          • Nice, thanks @Gokul M.
            – MSC
            Nov 8 at 6:23










          • Where is this documented in the official docs?
            – MSC
            Nov 8 at 9:04










          • This is nowhere documented. As this method is available in a module, we can include wherever needed.
            – Gokul M
            Nov 8 at 12:43










          • I'm not sure you're following my gist. The Rails documentation is not rigorous enough. How on earth is one supposed to learn this stuff?
            – MSC
            Nov 8 at 22:43











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201072%2frails-5-error-passing-time-ago-in-words-from-controller-to-view%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          The method time_ago_in_words is from ActionView::Helpers::DateHelper helper which will be automatically included in views.



          If you need to access this method from controller then, you need to include this helper in the controller.



          Assuming the controller name is CommentsController and action name as destroy:



          include ActionView::Helpers::DateHelper

          class CommentsController < ApplicationController
          def destroy
          ...
          if @comment.status == "locked"
          render "comments/too-old", locals: {message: "You can't delete this comment now as it's more than #{time_ago_in_words(AGE.ago)} old."}
          end
          ...
          end
          end





          share|improve this answer





















          • Nice, thanks @Gokul M.
            – MSC
            Nov 8 at 6:23










          • Where is this documented in the official docs?
            – MSC
            Nov 8 at 9:04










          • This is nowhere documented. As this method is available in a module, we can include wherever needed.
            – Gokul M
            Nov 8 at 12:43










          • I'm not sure you're following my gist. The Rails documentation is not rigorous enough. How on earth is one supposed to learn this stuff?
            – MSC
            Nov 8 at 22:43















          up vote
          1
          down vote



          accepted










          The method time_ago_in_words is from ActionView::Helpers::DateHelper helper which will be automatically included in views.



          If you need to access this method from controller then, you need to include this helper in the controller.



          Assuming the controller name is CommentsController and action name as destroy:



          include ActionView::Helpers::DateHelper

          class CommentsController < ApplicationController
          def destroy
          ...
          if @comment.status == "locked"
          render "comments/too-old", locals: {message: "You can't delete this comment now as it's more than #{time_ago_in_words(AGE.ago)} old."}
          end
          ...
          end
          end





          share|improve this answer





















          • Nice, thanks @Gokul M.
            – MSC
            Nov 8 at 6:23










          • Where is this documented in the official docs?
            – MSC
            Nov 8 at 9:04










          • This is nowhere documented. As this method is available in a module, we can include wherever needed.
            – Gokul M
            Nov 8 at 12:43










          • I'm not sure you're following my gist. The Rails documentation is not rigorous enough. How on earth is one supposed to learn this stuff?
            – MSC
            Nov 8 at 22:43













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          The method time_ago_in_words is from ActionView::Helpers::DateHelper helper which will be automatically included in views.



          If you need to access this method from controller then, you need to include this helper in the controller.



          Assuming the controller name is CommentsController and action name as destroy:



          include ActionView::Helpers::DateHelper

          class CommentsController < ApplicationController
          def destroy
          ...
          if @comment.status == "locked"
          render "comments/too-old", locals: {message: "You can't delete this comment now as it's more than #{time_ago_in_words(AGE.ago)} old."}
          end
          ...
          end
          end





          share|improve this answer












          The method time_ago_in_words is from ActionView::Helpers::DateHelper helper which will be automatically included in views.



          If you need to access this method from controller then, you need to include this helper in the controller.



          Assuming the controller name is CommentsController and action name as destroy:



          include ActionView::Helpers::DateHelper

          class CommentsController < ApplicationController
          def destroy
          ...
          if @comment.status == "locked"
          render "comments/too-old", locals: {message: "You can't delete this comment now as it's more than #{time_ago_in_words(AGE.ago)} old."}
          end
          ...
          end
          end






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 4:24









          Gokul M

          1,50311228




          1,50311228












          • Nice, thanks @Gokul M.
            – MSC
            Nov 8 at 6:23










          • Where is this documented in the official docs?
            – MSC
            Nov 8 at 9:04










          • This is nowhere documented. As this method is available in a module, we can include wherever needed.
            – Gokul M
            Nov 8 at 12:43










          • I'm not sure you're following my gist. The Rails documentation is not rigorous enough. How on earth is one supposed to learn this stuff?
            – MSC
            Nov 8 at 22:43


















          • Nice, thanks @Gokul M.
            – MSC
            Nov 8 at 6:23










          • Where is this documented in the official docs?
            – MSC
            Nov 8 at 9:04










          • This is nowhere documented. As this method is available in a module, we can include wherever needed.
            – Gokul M
            Nov 8 at 12:43










          • I'm not sure you're following my gist. The Rails documentation is not rigorous enough. How on earth is one supposed to learn this stuff?
            – MSC
            Nov 8 at 22:43
















          Nice, thanks @Gokul M.
          – MSC
          Nov 8 at 6:23




          Nice, thanks @Gokul M.
          – MSC
          Nov 8 at 6:23












          Where is this documented in the official docs?
          – MSC
          Nov 8 at 9:04




          Where is this documented in the official docs?
          – MSC
          Nov 8 at 9:04












          This is nowhere documented. As this method is available in a module, we can include wherever needed.
          – Gokul M
          Nov 8 at 12:43




          This is nowhere documented. As this method is available in a module, we can include wherever needed.
          – Gokul M
          Nov 8 at 12:43












          I'm not sure you're following my gist. The Rails documentation is not rigorous enough. How on earth is one supposed to learn this stuff?
          – MSC
          Nov 8 at 22:43




          I'm not sure you're following my gist. The Rails documentation is not rigorous enough. How on earth is one supposed to learn this stuff?
          – MSC
          Nov 8 at 22:43


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201072%2frails-5-error-passing-time-ago-in-words-from-controller-to-view%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Academy of Television Arts & Sciences

          L'Équipe

          1995 France bombings