I am trying to get a button to change its name/action after a user clicks on it, but I can't get the Ajax to work. It seems to work after I refresh the page. I am using the jquery-rails gem v.1.0.12. Context: I am working with a has_many :through association: Users who want to join Groups through a Membership- here are the models, and some user methods that are used later:
我试图在用户点击它之后获得一个按钮来更改其名称/操作,但我无法让Ajax工作。我刷新页面后似乎工作。我正在使用jquery-rails gem v.1.0.12。上下文:我正在使用has_many:通过关联:想要通过成员身份加入Groups的用户是模型,以及稍后使用的一些用户方法:
class User < ActiveRecord::Base
has_many :memberships
has_many :groups, :through => :memberships
has_many :groups_as_owner, :class_name => "Group"
.
.
.
def member?(group)
memberships.find_by_group_id(group)
end
def join!(group)
memberships.create!(:group_id => group.id)
end
def leave!(group)
memberships.find_by_group_id(group).destroy
end
.
.
.
end
class Group < ActiveRecord::Base
has_many :memberships
has_many :members, :through => :memberships, :source => :user
belongs_to :owner, :class_name => "User", :foreign_key => :user_id
has_attached_file :photo, :styles => { :thumb => "100x100",
:small => "200x200" }
attr_accessible :name, :description, :private, :created_at, :group_id
attr_accessible :photo, :photo_file_name, :photo_content_type,
:photo_file_size, :photo_updated_at
end
class Membership < ActiveRecord::Base
attr_accessible :group_id
belongs_to :user
belongs_to :group
end
Here is the membership controller:
这是会员控制器:
class MembershipsController < ApplicationController
def create
@group = Group.find(params[:membership][:group_id])
current_user.join!(@group)
respond_to do |format|
format.html { redirect_to @group }
format.js
end
end
def destroy
@group = Membership.find(params[:id]).group
current_user.leave!(@group)
respond_to do |format|
format.html { redirect_to @group }
format.js
end
end
def index
@memberships = Membership.all
end
end
I am trying to implement an AJAX controlled Join/Leave button for groups with the following jquery: view/memberships/create.js.erb
我正在尝试为具有以下jquery的组实现AJAX控制的Join / Leave按钮:view / memberships / create.js.erb
$('#join_form').html("<%= escape_javascript(render('groups/leave')) %>");
view/memberships/destroy.js.erb
视图/成员/ destroy.js.erb
$('#join_form').html("<%= escape_javascript(render('groups/join')) %>");
the jquery above acts on the _join_form.html.erb partial that is displayed on a Group show page:
上面的jquery作用于组显示页面上显示的_join_form.html.erb部分:
<div id="join-form">
<% if current_user.member?(@group) %>
<%= render 'leave' %>
<% else %>
<%= render 'join' %>
<% end %>
</div>
'leave' partial /groups/_leave.html.erb
'leave'parting /groups/_leave.html.erb
<%= form_for current_user.memberships.find_by_group_id(@group),
:html => { :method => :delete },
:remote => true do |f| %>
<div class="actions"><%= f.submit "Leave" %></div>
<% end %>
'join' partial /groups/_join.html.erb
'join'partial /groups/_join.html.erb
<%= form_for current_user.memberships.
build(:group_id => @group.id),
:remote => true do |f| %>
<div><%= f.hidden_field :group_id %></div>
<div class="actions"><%= f.submit "Join" %></div>
<% end %>
Here is my view/layout/application.html.erb where I include various javascript stuff:
这是我的视图/ layout / application.html.erb,其中包含各种javascript内容:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= javascript_include_tag :all %>
<%= csrf_meta_tag %>
<%= render 'layouts/stylesheets' %>
</head>
<body>
<div class="container">
<%= render 'layouts/header'%>
<section class="round">
<%= yield %>
</section>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
<%= javascript_include_tag 'jquery','jquery.min','rails.validations' %>
<%= javascript_include_tag :all, :cache => true %>
</body>
</html>
Sorry for the volume of code, but I just wanted to be thorough because it seems a lot of SO questions have too little context. If you want more info I'd be happy to provide more. I am using jquery because it seems to my beginner view that prototype is on the way out, and also I am using a rails-validations gem that requires jquery. Please let me know if you see any problems as I have been poring over this for hours and can't figure it out. I put my javascript tags near the end of my application layout as per the advice of http://lindsaar.net/2010/5/9/Getting-Rails-3-Edge-with-jQuery-RSpec-and-Cucumber-using-RVM and the membership 'helper' methods member?, join!, and leave! are adapted from Michael Hartl's Ruby on Rails tutorial at http://ruby.railstutorial.org/chapters/ Thanks, Brian
很抱歉代码量很大,但我只是想彻底,因为似乎很多SO问题的上下文太少了。如果您想了解更多信息,我很乐意提供更多信息。我正在使用jquery,因为在我的初学者看来,原型正在出路,而且我正在使用需要jquery的rails-validations gem。如果你发现任何问题,请告诉我,因为我已经仔细研究了几个小时但无法弄明白。根据http://lindsaar.net/2010/5/9/Getting-Rails-3-Edge-with-jQuery-RSpec-and-Cucumber-using的建议,我将我的javascript标签放在应用程序布局的末尾附近-RVM和会员'帮助'方法成员?,加入!,然后离开!改编自Michael Hartl的Ruby on Rails教程http://ruby.railstutorial.org/chapters/谢谢,Brian
1 个解决方案
#1
0
try Membership.where( :group_id => group.id)
or even simper: group.memberships
instead of your find_by_something.
尝试Membership.where(:group_id => group.id)或甚至simper:group.memberships而不是find_by_something。
find_by, etc. in general are rather slow and IMHO, unclean.
find_by等一般来说相当慢,恕我直言,不洁净。
The latter is usually faster, but be sure to always eager load associations with Group.find(1).includes(:memberships)
as this dramatically increases performance.
后者通常更快,但一定要总是急于加载与Group.find(1).includes(:membership)的关联,因为这会大大提高性能。
#1
0
try Membership.where( :group_id => group.id)
or even simper: group.memberships
instead of your find_by_something.
尝试Membership.where(:group_id => group.id)或甚至simper:group.memberships而不是find_by_something。
find_by, etc. in general are rather slow and IMHO, unclean.
find_by等一般来说相当慢,恕我直言,不洁净。
The latter is usually faster, but be sure to always eager load associations with Group.find(1).includes(:memberships)
as this dramatically increases performance.
后者通常更快,但一定要总是急于加载与Group.find(1).includes(:membership)的关联,因为这会大大提高性能。