问题
I have a blogging application that has posts that can receive votes through a vote
action in the posts controller.
Because I allow voting in both the index and show views, I redirect_to :back
after a vote is made, as below.
def vote
# voting logic goes here
redirect_to :back
end
This redirects me to the correct page, but I want to redirect to the specific post within the page. To do this, I added an identifying anchor to my post partial div.
<div id="post_<%= post.id %>">
<%= post.content %>
</div>
How can I reference this in my redirect_to :back
? I tried the below, which doesn't work.
# this doesn't work!
redirect_to :back, anchor: "post_#{@post.id}"
Alternatively, if I were to use an if clause for the show and index views, how do I do that? I tried the below, which returns undefined method 'current_page' for VocabsController
.
#this doesn't work!
if current_page?(posts_path)
redirect_to posts_path(anchor: "post_#{@post.id}"
else
redirect_to @post
end
回答1:
Deep in Rails redirecting mechanism, :back
simply means redirect to whatever in HTTP_REFERER
environment variable (or raise an error if nothing in this variable). So before redirect_to :back
in your controller add this line: env["HTTP_REFERER"] += '#some-id'
, this should do it.
回答2:
EDIT: ok my bad, got confused. you should look into this:
What's the right way to define an anchor tag in rails?
EDIT2:
the problem you have is that you are calling a helper inside a controller and not inside a view.
I encourage you to look into the view_context
method
http://jhonynyc.tumblr.com/post/5361328463/use-view-context-inside-rails-3-controller
回答3:
I ended up using Javascript instead.
posts_controller.rb
def vote
@post = Post.find(params[:id])
#voting logic
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
posts/_post.html.erb
<div class="post_partial" id="post_<%= post.id %>">
<%= post.content %>
<div id="vote_button_<%= post.id %>">
<%= link_to "up", vote_path, method: "post", remote: true %>
</div>
</div>
posts/vote.js.erb
$('#post_<%= @post.id %>').html('<%= j render @post %>');
回答4:
Under the hood, redirect_back(fallback_location: 'some_path')
redirects_to request.referrer
and falls back to 'some_path' when things go wrong.
Unfortunately, it does not provide for supplying an anchor. You can work around this by writing a simple private method to simulate it.
def back_with_anchor(anchor: '')
"#{request.referrer}##{anchor}"
end
You can then do this where needed.
redirect_to back_with_anchor anchor: @record.id
来源:https://stackoverflow.com/questions/13793736/how-to-add-an-anchor-to-redirect-to-back