我正在尝试向用户创建的事件添加加入/取消加入按钮,类似于用户的关注/取消关注按钮。
我不确定在事件#show 中如何定义@rsvps
事件中的名称错误#show undefined local variable or method `event' for #<#:0x007f9dfaf9d978>
显示.html.erb
<%= link_to "Join Event", rsvps_path(:event_id => event), :method => :post %>
events_controller.rb
def show
@event = Event.find(params[:id])
@user = current_user
#@rsvp = ???? something here ????
end
rsvps_controller.rb
class RsvpsController < ApplicationController
before_filter :signed_in_user
def create
@rsvp = current_user.rsvps.build(:event_id => params[:event_id])
if @rsvp.save
flash[:notice] = "Joined event."
redirect_to root_url
else
flash[:error] = "Unable to join event."
redirect_to root_url
end
end
def destroy
@rsvp = current_user.rsvps.find(params[:id])
@rsvp.destroy
flash[:notice] = "Unjoin Event."
redirect_to current_user
end
end
这里是模型
rsvp.rb
class Rsvp < ActiveRecord::Base
attr_accessible :event_id, :user_id
belongs_to :user
belongs_to :event
end
用户.rb
has_many :rsvps
has_many :events, through: :rsvps, dependent: :destroy
事件.rb
belongs_to :user
has_many :rsvps
has_many :users, through: :rsvps, dependent: :destroy