在Rails的教程3§12.2.5 - 巴顿没有在阿贾克斯更新 [英] Button doesn't update in Ajax - Rails Tutorial 3 at §12.2.5
问题描述
我要通过Rails教程由迈克尔·哈特尔和打一个小障碍,在§12.2.5在这里我们应该创建一个工作按钮使用Ajax。我知道code是正确的(我结束了直接从书中抄袭,并重新输入三次),我的绿色。不过,这并不实际工作!
I'm going through the Rails Tutorial by Michael Hartl and hit a small snag at §12.2.5 where we're supposed to create a working button with Ajax. I know the code is correct (I wound up copying it directly from the book and retyping it three times) and I'm green. But it doesn't actually work!
通过本部分教程,我们正在改变常规的形式提交按钮与阿贾克斯工作,使整个页面不刷新(真的,重定向到同一页),而不是仅仅的按钮和一个相应的侧边栏项目更新。问题是按钮不会自动重新加载后,点击因为我expectng。它在重新加载页面刷新。如果我在我的浏览器禁用JS,它将恢复 - 因为它应该 - 以触发重定向和刷新整个页面的HTML版本。如果你想知道,我已经尝试刷新页面,我已经尝试过Firefox,Safari和Chrome浏览器和Chrome在隐身。我已经关闭并重新启动Rails服务器,叉勺和自动测试。我已经重写所有的code,然后复制所有的书的code。还是难住了。
Through this part of the tutorial we are changing a regular form submit button to work with Ajax so that the entire page doesn't "refresh" (really, redirect to the same page) and instead just the button and a corresponding sidebar item update. The problem is the button doesn't automatically reload upon clicking as I'm expectng. It does reload upon a page refresh. If I disable JS in my browser, it reverts - as it should - to the HTML version that triggers a redirect and "refreshes" the entire page. If you're wondering, I've tried refreshing the page and I've tried Firefox, Safari and Chrome and Chrome in Incognito. I've shut down and restarted the rails server, spork and autotest. And I've rewritten all the code and then copied all of the book's code. Still stumped.
因此,即使测试是绿色的,我有一个不完美的工作职能。也许有我丢失的东西,你能不能帮我找到它?也许我错过了javascreipt相关的宝石,一个Ajax宝石...
So even though the test is green I have an imperfectly working function. Maybe there's something I'm missing and you can help me find it? Maybe I'm missing a javascreipt-related gem, an Ajax gem...
的code相关部分:
relationships_controller.erb:
relationships_controller.erb:
class RelationshipsController < ApplicationController
before_filter :authenticate
def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
结束
def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
end
_follow.html.erb:
_follow.html.erb:
&LT;%=的form_for(current_user.relationships。
建设(:followed_id => @ user.id)
:远程=>真)并| F | %>&LT;%=
f.hidden_field:followed_id%>
&其中;%= f.submit
按照%>&LT;%结束%>
<%= form_for(current_user.relationships. build(:followed_id => @user.id), :remote => true) do |f| %> <%= f.hidden_field :followed_id %>
<%= f.submit "Follow" %> <% end %>
_unfollow.html.erb:
_unfollow.html.erb:
<%= form_for(current_user.relationships.find_by_followed_id(@user),
:html => { :method => :delete },
:remote => true) do |f| %>
&LT;%结束%>
<% end %>
create.js.erb
create.js.erb
$("follow_form").update("<%= escape_javascript(render('users/unfollow')) %>")
$("followers").update('<%= "#{@user.followers.count} followers" %>')
destroy.js.erb
destroy.js.erb
$("follow_form").update("<%= escape_javascript(render('users/follow')) %>")
$("followers").update('<%= "#{@user.followers.count} followers" %>')
我也确信,这条线是包含在application.html.erb
I also made sure that this line is included in the application.html.erb
<%= javascript_include_tag :defaults %>
如果有需要审时度势别的,请你,我会和更新作出反应。
If there's anything else you need to assess the situation, please ask and I'll respond with an update.
TIA
推荐答案
您使用的是什么版本的轨道?如果它的3.1必须更改
what version of rails are you using? If its 3.1 you have to change
create.js.erb为:
create.js.erb to:
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>")
$("#followers").html('<%= "#{@user.followers.count} followers" %>')
和destroy.js.erb为:
and destroy.js.erb to:
$("#follow_form").html("<%= escape_javascript(render('users/follow')) %>")
$("#followers").html('<%= "#{@user.followers.count} followers" %>')
(注意额外的#) 其对本教程的最后一章,以改变导轨3.1的注意事项
(Notice the extra #) Its on the last chapter of the tutorial, the notes of changing to rails 3.1
这篇关于在Rails的教程3§12.2.5 - 巴顿没有在阿贾克斯更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!