ruby on rails - Deleting an associated record through a remote_form -
i have built ruby on rails app lets users track workouts. user has_many workouts. in addition, user can create box (gym) if gym owner. users can associate box through membership resource.
i create association current_user remote form in /views/boxes/show.html.erb:
<% remote_form_for membership.new |f| %> <%= f.hidden_field :box_id, :value => @box.id %> <%= f.hidden_field :user_id, :value => current_user.id %> <%= submit_tag "i member of box" , :class => '' %> <% end %>
i want give current_user ability remove association. how structure link/form , should memberships_controller destroy action like?
below list of associations if helps: user
class user < activerecord::base has_many :boxes has_many :workouts, :dependent => :destroy end
workout
class workout < activerecord::base belongs_to :user belongs_to :box end
box
class box < activerecord::base belongs_to :user has_many :users, :through => :memberships has_many :workouts, :through => :users has_many :memberships end
membership
class membership < activerecord::base belongs_to :user belongs_to :box end
after have done changes suggested here, , assuming have obtained membership current_user , box using following code,
@membership = current_user.memberships.for_box(box).first
create link user delete membership follows.
link_to 'delete membership', membership_path(@membership), :method => :delete
membership controller should have destroy method looks following.
def destroy @membership = membership.find(params[:id]) @membership.destroy end
this assuming memberships controller restful , have map.resources :memberships
in routes.
Comments
Post a Comment