在 Ruby on Rails 中仅删除连接表记录

发布于 2024-11-26 10:14:09 字数 611 浏览 1 评论 0原文

我有以下简单的模型:

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

在我看来,我想做的是,根据当前用户的角色,删除事件及其参与记录,或者仅删除参与记录本身。

我目前有

<%= link_to '删除事件', event, :confirm => '你确定吗?', :方法=> :删除%>

这会删除事件及其参与。我还需要采取其他行动吗?或者可以劫持Event的销毁动作?它会是什么样子?

谢谢

I have the following simple models:

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.

I currently have

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

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?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2024-12-03 10:14:09

嗯,在视图助手中,黑客可能是这样的:

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( event,participation )< /strong> 删除参与。您的控制器可能是这样的:

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

编辑

为了减少黑客攻击,您应该为事件下的参与创建一个嵌套资源:

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

然后您必须创建一个ParticipationsController >,它可能看起来像这样:

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 帮助器将更改为:

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

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

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

EDIT

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文