Ruby on rails Rails有很多:通过关联。是否删除与链接的关联?
我有一个事件模型和一个用户模型,它们通过一个与会者模型连接在一起。我已经了解了如何以身份验证用户的身份“参加”活动。但我想不出一个“退出”活动的好方法。我确信这是我遗漏的一些琐碎的事情,但是有什么比问一些琐碎的事情更好的进入StackOverflow的方法呢?哦,我一直在搜索铁路乘客,所以几个小时 谢谢 views/events/show.html.erbRuby on rails Rails有很多:通过关联。是否删除与链接的关联?,ruby-on-rails,ruby,activerecord,has-many-through,Ruby On Rails,Ruby,Activerecord,Has Many Through,我有一个事件模型和一个用户模型,它们通过一个与会者模型连接在一起。我已经了解了如何以身份验证用户的身份“参加”活动。但我想不出一个“退出”活动的好方法。我确信这是我遗漏的一些琐碎的事情,但是有什么比问一些琐碎的事情更好的进入StackOverflow的方法呢?哦,我一直在搜索铁路乘客,所以几个小时 谢谢 views/events/show.html.erb <p><strong>Attendees: </strong> <ul>
<p><strong>Attendees: </strong>
<ul>
<% for attendee in @event.users %>
<% if attendee.username == current_user.username %>
<li><strong><%= attendee.username %></strong>
<%= link_to 'Withdraw From Event', withdraw_event_path(@event.id), :method => :post, :class => 'btn btn-danger' %>
<%= link_to 'Destroy', @attendee, confirm: 'Are you sure?', method: :delete, :class => 'btn btn-danger' %>
</li>
<% else %>
<li><%= attendee.username %></li>
<% end %>
<% end %>
</ul>
</p>
def attend
@event = Event.find(params[:id])
current_user.events << @event
redirect_to @event, notice: 'You have promised to attend this event.'
end
def withdraw
# I can't get this to work
redirect_to @event, notice: 'You are no longer attending this event.'
end
class Event < ActiveRecord::Base
attr_accessible :name, :location
belongs_to :users
has_many :attendees, :dependent => :destroy
has_many :users, :through => :attendees
class User < ActiveRecord::Base
has_many :events
has_many :attendees, :dependent => :destroy
has_many :events, :through => :attendees
class Attendee < ActiveRecord::Base
belongs_to :event
belongs_to :user
attr_accessible :user_id, :event_id
# Make sure that one user cannot join the same event more than once at a time.
validates :event_id, :uniqueness => { :scope => :user_id }
end
与会者:
-
:post,:class=>“btn btn危险”%>
“btn btn危险”%>
/controllers/events\u controller.rb
<p><strong>Attendees: </strong>
<ul>
<% for attendee in @event.users %>
<% if attendee.username == current_user.username %>
<li><strong><%= attendee.username %></strong>
<%= link_to 'Withdraw From Event', withdraw_event_path(@event.id), :method => :post, :class => 'btn btn-danger' %>
<%= link_to 'Destroy', @attendee, confirm: 'Are you sure?', method: :delete, :class => 'btn btn-danger' %>
</li>
<% else %>
<li><%= attendee.username %></li>
<% end %>
<% end %>
</ul>
</p>
def attend
@event = Event.find(params[:id])
current_user.events << @event
redirect_to @event, notice: 'You have promised to attend this event.'
end
def withdraw
# I can't get this to work
redirect_to @event, notice: 'You are no longer attending this event.'
end
class Event < ActiveRecord::Base
attr_accessible :name, :location
belongs_to :users
has_many :attendees, :dependent => :destroy
has_many :users, :through => :attendees
class User < ActiveRecord::Base
has_many :events
has_many :attendees, :dependent => :destroy
has_many :events, :through => :attendees
class Attendee < ActiveRecord::Base
belongs_to :event
belongs_to :user
attr_accessible :user_id, :event_id
# Make sure that one user cannot join the same event more than once at a time.
validates :event_id, :uniqueness => { :scope => :user_id }
end
def出席
@event=event.find(参数[:id])
current_user.events我想你找不到与会者了
def withdraw
event = Event.find(params[:id])
attendee = Attendee.find_by_user_id_and_event_id(current_user.id, event.id)
if attendee.blank?
# handle case where there is no matching Attendee record
end
attendee.delete
redirect_to event, notice: 'You are no longer attending this event.'
end
就这样!我知道这很琐碎。我忘了你可以写一个这样的方法,Rails还是Active Record?我会把它拼起来的。我希望其他人会发现这个简单的修复很有用。我有非常相似的代码,但我找不到retrach\u event\u路径(@event.id)-关于修复这个未定义方法的任何想法没有更好的方法吗?Attendee.find_by_user_id_和_event_id(当前_user.id,event.id)看起来很难看:/@FlorianWidtmann是的,很难看。:-)Rails 4中也不推荐使用它。您可以使用Attendee.find\u by(用户标识:current\u user.id,事件标识:event.id)