So, I have 2 follow/unfollow forms in my Rails code. Which are like this
所以,我的Rails代码中有2个关注/取消关注表单。这是这样的
<%= form_tag( {:controller => 'profile', :action => 'unfollow_topic' }) do %>
<%= hidden_field_tag :topic_id, @topic.id %>
<button class="question_button" type="submit">Unfollow</button>
<% end %>
and this:
<%= form_tag({:controller => 'profile', :action => 'follow_topic' }) do %>
<%= hidden_field_tag :topic_id, @topic.id %>
<button class="question_button" type="submit">Follow</button>
<% end %>
And then I have this javascript code:
然后我有这个javascript代码:
<script type="text/javascript">
$(function(){
$('#follow form').submit(function(e) {
$.ajax({
url: this.action,
type: 'POST',
data: $(this).serialize(),
success: function(response){ $('#follow').html(response); }
});
return false;
});
});
</script>
My question is how i can return from my controller the appropriate "follow/unfollow" partial for it to be replaced with the response returned.
我的问题是我如何从我的控制器返回适当的“跟随/取消关注”部分,以便将其替换为返回的响应。
What I currently have is this, but it doesn't seems to work:
我目前拥有的是这个,但它似乎不起作用:
respond_to do |format|
format.html { redirect_to('/' + topic.type + '/' + topic.uri) }
format.js { render :partial => 'unfollow_topic' }
end
But instead if renders the whole page, I believe it's actually responding to the format.html and not the format.js any ideas?
但相反,如果渲染整个页面,我相信它实际上是对format.html而不是format.js的任何想法做出回应?
1 个解决方案
#1
5
JavaScript:
<script type="text/javascript">
$(function(){
$('#follow form').submit(function(e) {
$.ajax({
url: this.action,
type: 'POST',
data: $(this).serialize(),
dataType: 'html'
success: function(response){ $('#follow').html(response); }
});
return false;
});
});
</script>
controller:
respond_to do |format|
format.html do
if request.xhr?
render :partial => 'unfollow_topic'
else
redirect_to('/' + topic.type + '/' + topic.uri)
end
end
end
#1
5
JavaScript:
<script type="text/javascript">
$(function(){
$('#follow form').submit(function(e) {
$.ajax({
url: this.action,
type: 'POST',
data: $(this).serialize(),
dataType: 'html'
success: function(response){ $('#follow').html(response); }
});
return false;
});
});
</script>
controller:
respond_to do |format|
format.html do
if request.xhr?
render :partial => 'unfollow_topic'
else
redirect_to('/' + topic.type + '/' + topic.uri)
end
end
end