Ruby on rails Rails 4允许用户创建和参加活动
我在GIThub上发现了这个小应用程序,它允许用户在这里创建出席和取消活动:唯一的问题是它是为Rails 3开发的。我正在尝试将该应用程序实现到我自己的实践应用程序中,但使用rails 4.2.4到目前为止,我在参加活动功能方面遇到了问题 我有三个模型,它们使用丰富的连接进行连接,使用has-many-through:user、event和event\u注册Ruby on rails Rails 4允许用户创建和参加活动,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,activerecord,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,Activerecord,我在GIThub上发现了这个小应用程序,它允许用户在这里创建出席和取消活动:唯一的问题是它是为Rails 3开发的。我正在尝试将该应用程序实现到我自己的实践应用程序中,但使用rails 4.2.4到目前为止,我在参加活动功能方面遇到了问题 我有三个模型,它们使用丰富的连接进行连接,使用has-many-through:user、event和event\u注册 user.rb class User < ActiveRecord::Base has_many :event_registrat
user.rb
class User < ActiveRecord::Base
has_many :event_registrations
has_many :events, through: :event_registrations
创建新事件的功能位于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
这是创建事件的my events\show.html.erb的一个片段
<% 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表中根本没有新记录
提前感谢您的帮助。您需要有一个创建操作,并且需要将flash通知从新操作移动到创建中 您的创建操作应该如下所示
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
您必须有一个创建操作并删除新操作下的flash_通知。这就是为什么你得到的是成功信息,而不是保存。原作者说:一个用于学习协会的Rails应用程序。所以它可能更具实验性。我的建议是不要把它作为你自己系统的基础谢谢@Meier,但我自己只是在练习rails,所以它根本不会投入生产。这就是为什么你应该使用一个好的工作示例来学习,而不是一个状态未知、错误未知的实验示例至少有一个Hanks@Russell,不幸的是,event_registrations controller没有任何视图,因此我得到了缺少的模板错误。当用户单击“参加”链接时,可直接从事件显示页面Events\show.html.erb注册新的事件。由于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 %>
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