Javascript jQuery/Rails 3按钮不会更改状态
我花了几天时间(比如4天)试图解决这个问题。我遵循HartlRails3教程,在第12章中尝试将站点从原型转换为jQuery。但是,我无法更新“跟随”/“取消跟随”按钮 我可以在Safari的Inspect元素控制台中发出jQuery命令,但是如果我将最简单的jQuery命令放入destroy.js.erb或create.js.erb文件中,什么也不会发生。日志指示正在渲染适当的关系/destry.js.erb(或create.js.erb)文件 以下是我在控制器中的代码:Javascript jQuery/Rails 3按钮不会更改状态,javascript,jquery,ruby-on-rails-3,railstutorial.org,Javascript,Jquery,Ruby On Rails 3,Railstutorial.org,我花了几天时间(比如4天)试图解决这个问题。我遵循HartlRails3教程,在第12章中尝试将站点从原型转换为jQuery。但是,我无法更新“跟随”/“取消跟随”按钮 我可以在Safari的Inspect元素控制台中发出jQuery命令,但是如果我将最简单的jQuery命令放入destroy.js.erb或create.js.erb文件中,什么也不会发生。日志指示正在渲染适当的关系/destry.js.erb(或create.js.erb)文件 以下是我在控制器中的代码: class Rela
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
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
users/\u unfollow.html.haml是
- relationship = current_user.relationships.find_by_followed_id(@user)
- delete = { :method => :delete }
= form_for(relationship, :html => delete, :remote => true) do |f|
.actions= f.submit "Unfollow"
- unless current_user?(@user)
#follow_form
- if current_user.following?(@user)
= render 'unfollow'
- else
= render 'follow'
用户/_follow_form.html.haml是
- relationship = current_user.relationships.find_by_followed_id(@user)
- delete = { :method => :delete }
= form_for(relationship, :html => delete, :remote => true) do |f|
.actions= f.submit "Unfollow"
- unless current_user?(@user)
#follow_form
- if current_user.following?(@user)
= render 'unfollow'
- else
= render 'follow'
关系/create.js.erb是
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>")
$("#followers").html('<%= "#{@user.followers.count} followers" %>')
这也不起作用 看起来是关于如何使用jQuery的问题 jQuery使用CSS样式选择器,而Prototype不使用。因此,要使其正常工作,只需在选择器中添加一个“#”符号 而不是
$("follow_form").html("something");
你应该使用
$("#follow_form").html("something");
// Note the # symbol in the selector.
// Also, remember to end your statements with a semicolon.
您可以在此处阅读有关jQuery ID选择器的信息:检查您的公用文件夹中是否有资产文件夹,其中包含application.js。不知道我是如何结束的,但这导致ajax查询被处理了两次 看起来您可能正在使用Rails 3.1。如果想要获得相同的结果,使用本教程中使用的gem版本(包括Rails 3.0而不是3.1)是很重要的。有关更多建议,请参阅上的调试提示。这是一个很好的提示,但即使在更改之后问题仍然存在。还有其他想法吗?我能做些什么来帮助诊断?
$("#follow_form").html("something");
// Note the # symbol in the selector.
// Also, remember to end your statements with a semicolon.