Rails 4允许用户创建和参加事件 [英] Rails 4 Allow Users to Create and Attend Events
问题描述
我在GIThub上找到了这个微型应用程序,它允许用户在此处创建出席和取消活动: https://github.com/ghembo/private-events 唯一的问题是它是为Rails 3制作的.我正在尝试将该应用程序实现到我自己的实践应用程序中,但到目前为止,我使用的是Rails 4.2.4参加活动功能有问题.
I found this mini-application on GIThub that allows users to create attend and cancel events here: https://github.com/ghembo/private-events the only issue is that it was made for Rails 3. I'm trying to implement that app into my own practice application but using rails 4.2.4 so far i'm having issues with the attend events functionality.
我有三种使用富连接进行连接的模型,其中使用了许多连接:user,event和event_registration.
I have three models that are connected using a rich join using has many through: user, event, and event_registration.
user.rb
class User < ActiveRecord::Base
has_many :event_registrations
has_many :events, through: :event_registrations
.
event.rb
class Event < ActiveRecord::Base
has_many :event_registrations
has_many :users, through: :event_registrations
.
event_registration.rb
class EventRegistration < ActiveRecord::Base
belongs_to :event
belongs_to :user
创建新事件的功能位于event_registrations_controller.rb中
The functionality that creates a new event is located within event_registrations_controller.rb
class EventRegistrationsController < ApplicationController
def new
EventRegistration.new(event_id: params[:event_id].to_i, user_id:
current_user.id)
redirect_to event_path(params[:event_id])
flash[:notice] = "Thanks for attending this event!"
end
def destroy
EventRegistration.where(event_id: params[:event_id].to_i, user_id:
current_user.id).first.destroy
redirect_to event_path(params[:event_id])
end
private
# Use callbacks to share common setup or constraints between actions.
def set_event_registration
@event_registration = EventRegistration.find(params[:id])
end
def event_registration_params
params.require(:event_registration).permit(:user_id, :event_id)
end
end
这是创建事件的我的events \ show.html.erb的摘要.
This is a snippet of my events\show.html.erb that creates the event.
<% if session[:user_id] %>
<% if @event.attended_by(User.find(session[:user_id])) %>
<%= link_to "Cancel attendance", event_registration_path(event_id:
@event.id), method: :delete, class: "btn
btn-primary" %>
<% else %>
<%= link_to "Attend", new_event_registration_path(event_id: @event.id),
class: "btn btn-success" %>
<% end %>
<% end %>
问题是我确实收到了感谢您参加此活动!"消息,但在event_registrations表中根本没有任何新记录.
The problem is that I do get the "Thanks for attending this event!" message but no new record is made in the event_registrations table at all.
预先感谢您的帮助.
推荐答案
您需要具有创建操作,并且需要将Flash通知从新操作移至创建中.
You need to have a create action and will need to move the flash notice from the new action into the create.
您的创建动作应如下所示
Your create action should look something like this
def new
@event_registration = EventRegistration.new
end
def create
@event_registration = current_user.events.build(event_registration_params)
if @event_registration.save
flash[:notice] = 'Event registration created'
redirect_to events_path
else
flash.now[:warning] = 'There were problems when trying to create a new event registration'
render :action => :new
end
end
这篇关于Rails 4允许用户创建和参加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!