删除Ruby on Rails中的联接表记录 [英] Deleting just a join table record in Ruby on Rails

查看:74
本文介绍了删除Ruby on Rails中的联接表记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单模型:

class Event < ActiveRecord::Base
  has_many :participations
  has_many :users, :through => :participations
end

class Participation < ActiveRecord::Base
  belongs_to :event
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :participations
  has_many :events, :through => :participations
end

我想做的是,取决于当前用户角色,删除事件及其参与记录,或仅删除其参与记录。

What I would like to do in my view is, dependant on the current users role, delete either an event and its participation record, or just a participation record on its own.

我目前拥有


<%= link_to'删除事件',event,:confirm =>'您确定吗?',
:method =>:delete%>

<%= link_to 'Delete event', event, :confirm => 'Are you sure?', :method => :delete %>

会同时删除事件及其参与。我需要采取其他行动吗?还是可以劫持Event的破坏行为?会是什么样?

which deletes both event, and its participation. Do I need another action? or can hijack the destroy action of Event? What would it look like?

谢谢

推荐答案

hack可能是这样的,在视图助手中:

Well, a hack could be something like this, in a view helper:

def link_to_delete_event( event, participation = nil )
  final_path = participation.nil? ? event_path( event ) : event_path( :id => event, :participation_id => participation )
  link_to 'Delete event', final_path, :confirm => 'Are you sure?', :method => :delete
end

在您看来,您可以使用 link_to_delete_event(event )仅删除一个活动,而 link_to_delete_event(事件,参与)则删除该参与。您的控制器可能是这样的:

And in your view you'd use link_to_delete_event( event ) to delete an event alone and link_to_delete_event( event, participation ) to delete the participation. Your controller could be something like this:

def destroy
  @event = Event.find(params[:id])
  unless params[:participation_id].blank?
    @event.destroy
  else
    @event.participations.find( params[:participation_id] ).destroy
  end
  redirect_to somewhere_path
end

编辑

为了减少麻烦,您应该为事件下的参与创建嵌套资源:

To make it less of a hack you should create a nested resource for participations under events:

map.resources :events do |events|
  events.resources :participations
end

然后您必须创建一个 ParticipationsController ,如下所示:

And then you'll have to create a ParticipationsController, which could look like this:

class ParticipationsController < ApplicationController
  before_filter :load_event

  def destroy
    @participation = @event.participations.find( params[:id] )
    @participation.destroy
    redirect_to( event_path( @event ) )
  end

  protected

  def load_event
    @event = Event.find( params[:event_id] )
  end
end

然后link_to助手将更改为:

And the link_to helper would change to this:

def link_to_delete_event( event, participation = nil )
  if participation
    link_to 'Remove participation', event_participation_path( event, participation ), :method => :delete
  else
    link_to 'Delete event', event_path( event ), :confirm => 'Are you sure?', :method => :delete
  end
end

这篇关于删除Ruby on Rails中的联接表记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆