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:
class User < ActiveRecord::Base
has_many :memberships
has_many :groups, :t开发者_如何学Gohrough => :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
$('#join_form').html("<%= escape_javascript(render('groups/leave')) %>");
view/memberships/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:
<div id="join-form">
<% if current_user.member?(@group) %>
<%= render 'leave' %>
<% else %>
<%= render 'join' %>
<% end %>
</div>
'leave' partial /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
<%= 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:
<!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
try
Membership.where( :group_id => group.id)
or even simper:
group.memberships
instead of your find_by_something.
find_by, etc. in general are rather slow and IMHO, unclean.
The latter is usually faster, but be sure to always eager load associations with Group.find(1).includes(:memberships)
as this dramatically increases performance.
精彩评论