Reputation: 852
I am using a comments model in several associations to other models. The relationship is always the same: a comment "belongs to" one other model's item, and those other models "have many" comments.
I am rendering the comment form on the other models' show pages like so:
<%= render "comments/form" %>
When a user clicks the submit button, I would like him to be redirected to different pages, depending on which page he was on before. As I searched for a solution I found the current_page?
ActionView helper via APIdock (cf. http://apidock.com/rails/v4.2.1/ActionView/Helpers/UrlHelper/current_page%3F) and thought it was a good idea to use it in the comments controller like this:
respond_to do |format|
if (@comment.save) && (current_page?(controller: 'game', action: 'show'))
format.html { redirect_to game_comments_url, notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: @comment }
elsif (@comment.save) && (current_page?(controller: 'comment'))
format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: @comment }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
But sadly I get the following error message:
undefined method `current_page?' for #<CommentsController:0x007f67fe42e1a0>
I would appreciate if someone can present me a solution on how I can find out, on which page a particular form was submitted on, or at least a way on how to redirect users to different pages then.
Upvotes: 0
Views: 349
Reputation: 951
current_page?
is a view helper, so it's not available in controller.
you can get refer url by request.referrer
in controller
Upvotes: 1