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'
ruby-on-rails
add a comment |
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'
ruby-on-rails
add a comment |
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'
ruby-on-rails
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
ruby-on-rails
edited Nov 8 at 3:24
asked Nov 8 at 3:18
MSC
1,60321526
1,60321526
add a comment |
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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