关于 Event_calendar.仅显示当前用户的事件而不是所有事件 [英] About Event_calendar.Showing only events for current user and not all events present

查看:41
本文介绍了关于 Event_calendar.仅显示当前用户的事件而不是所有事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我安装了 https://github.com/elevation/提供的 event_calendar 解决方案event_calendar 一切正常.现在我意识到我将有几个用户,并且现在我的任何用户都能够看到ALL数据库中的现有事件.

我需要让每个用户只看到他参与的事件;所以我想再创建一个名为 XXXXXXXXX_create_events_users_join.rb 的迁移,在那里我只保留用户和事件的 ID.那么我应该如何在实际日历上只显示属于当前用户的事件.或者是吗创建迁移不是最好的方法?

解决方案

您的解决方案可能有效,但它比实际需要复杂得多.Rails 有一种很好的内部管理方式.现在你需要做的是:

1-> 创建迁移:add_user_id_to_events user_id:integer

2-> 设置关系:

在用户模型(user.rb)中:

<块引用>

class User 

在事件模型 (event.rb) 中:

<块引用>

class 事件 

3-> 然后在您的事件视图中,您可以使用例如:

<块引用>

<代码> ...<% @user.events.each 做 |event|%><tr><td><%= event.name %></td>....</tr><%结束%>...

对于日历,您只需要更改:

<块引用>

@event_strips = Events.event_strips_for_month(@shown_month)

类似的东西

<块引用>

@event_strips = current_user.events.event_strips_for_month(@shown_month)

希望我有所帮助!

问候

A couple of days ago I installed the event_calendar solution that is offered by https://github.com/elevation/event_calendar and everything is working fine.Now I've realized that I'm going to have several users and being things as they're right now any of my users is able to see ALL existing events in the db.

What I need is for each user is to only see the events he's being part of; so I thought to create one more migration called XXXXXXXXX_create_events_users_join.rb where I would only keep the users' and the events' ids.So how am I supposed to show on the actual calendar only the events that belong to the current user.Or is it creating that migration not the best approach?

解决方案

Your solution might work but it is much more complex than needed. Rails has a nice way of managing such thing internaly. Now what you need to do is:

1-> Create a migration: add_user_id_to_events user_id:integer

2-> Set the relationships:

in User model (user.rb):

class User < ActiveRecord::Base
    has_many :events
    ....
end

in Event model (event.rb):

class Event < ActiveRecord::Base
   has_event_calendar
   belongs_to user
   ....
end

3-> Then in your events view you can use for example:

 ...
 <% @user.events.each do |event| %>
    <tr>
       <td><%= event.name %></td>
       ....
    </tr>
 <% end %>
 ...

and for the calendar you just need to change:

@event_strips = Events.event_strips_for_month(@shown_month) 

to something like

@event_strips = current_user.events.event_strips_for_month(@shown_month) 

Hope I helped!

Regards

这篇关于关于 Event_calendar.仅显示当前用户的事件而不是所有事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆