Rails:视图中两个属性不同的表

发布于 2024-11-04 17:15:17 字数 1447 浏览 0 评论 0原文

我一直在尝试使用来自两个不同模型的两个属性,以便它们可以与索引视图中 table_builder 插件提供的 calendar_for 方法一起使用。我已经阅读了 http://guides.rubyonrails.org/ 的 Rails 指南active_record_querying.html#retriving-multiple-objects 以及诸如 Ruby on Rails:如何连接两个表但是我一定做错了什么。

我的模型如下:

Class Event < ActiveRecord::Base
belongs_to :user

我在控制器中尝试过的不同

class User < ActiveRecord::Base
User has_many :events

方法(不是一次全部)是:

@event.user.name
@users = User.joins(:event).where(:event => {:event_date => true})
@users = User.where(:event => :event_date)

在其他人中,我的视图如下所示:

我的视图代码:

<% calendar_for @users, :year => @date.year, :month => @date.month do |calendar| %>
<%= calendar.head('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',   'Saturday') %>
<% calendar.day(:day_method => :created_at) do |date, users| %>
<%= date.day %>
  <ul>
    <% for user in users %>
      <li><%= link_to h(user.name), user %></li>
    <% end %>
  </ul>
<% end %>
<% end %>
</div>

我已尝试相应地更改视图中的变量,但无济于事。我想在预订活动的特定日期显示用户姓名和指向用户的链接。

I have been trying trying to use two attributes from two different models so they can be used with the calendar_for method provided by the table_builder plugin in the index view. I have been through the Rails guides for http://guides.rubyonrails.org/active_record_querying.html#retrieving-multiple-objects and posts such as Ruby on Rails: How to join two tables however i must be doing something wrong.

My models are as follows:

Class Event < ActiveRecord::Base
belongs_to :user

and

class User < ActiveRecord::Base
User has_many :events

The different ways I have tried in the controller (not all at once) are:

@event.user.name
@users = User.joins(:event).where(:event => {:event_date => true})
@users = User.where(:event => :event_date)

Amoung others, my view looks like:

my view code:

<% calendar_for @users, :year => @date.year, :month => @date.month do |calendar| %>
<%= calendar.head('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',   'Saturday') %>
<% calendar.day(:day_method => :created_at) do |date, users| %>
<%= date.day %>
  <ul>
    <% for user in users %>
      <li><%= link_to h(user.name), user %></li>
    <% end %>
  </ul>
<% end %>
<% end %>
</div>

I have tried changing the variables in the view accordingly however to no avail. I would like to show the users name and a link to the user on the specific day that their event is booked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静赏你的温柔 2024-11-11 17:15:17

尽管我不熟悉日历库,但我看到了两个问题。

首先,确保控制器中的查询返回有用的内容。在您给我们的三行中,第一行甚至不搜索,它调用未定义变量的方法。第二个接近工作,但您正在搜索一个日期并将其与 true 匹配...怎么样:

@users = User.joins(:event).where('events.event_date is not null')

此外,如果您有一个日期范围,您可能会将其包含在搜索中:

@users = User.joins(:event).where('events.event_date > ? and events.event_date < ?', start_date, end_date)

接下来在视图中,您是'与您的变量命名一致。控制器设置了 @users 变量,您访问该变量一次,但稍后您会丢失它前面的 @,这不是同一回事。我不知道日历部分想要什么作为输入,但至少 for 循环应该是:

for user in @users

也就是说,for 循环不是很Ruby。 ruby 方法是使用 each:

@users.each do |user|
  ...
end

甚至更好,使所有链接:

<ul>
<%= @users.collect {|user| content_tag(:li, link_to h(user.name), user).join } %>
</ul>

edit

根据更多信息,我认为您从错误的地方开始。让我们从事件开始。

Event.joins(:users).where('events.event_date is not null')


<% calendar_for @events, :year => @date.year, :month => @date.month do |calendar| %>
  <%= calendar.head('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',   'Saturday') %>
  <% calendar.day(:day_method => :event_date) do |date, events| %>
    <%= date.day %>
    <ul>
      <% for event in events %>
        <li><%= link_to h(event.user.name), event.user %></li>
      <% end %>
    </ul>
  <% end %>
<% end %>

Two issues I see, though I'm not familiar with the calendar library.

First, make sure your query in the controller returns something useful. Of the three lines you gave us, the first doesn't even search, it calls methods on an undefined variable. The second is close to working, but you are searching for a date and matching it to true... How about:

@users = User.joins(:event).where('events.event_date is not null')

Furthermore, if you have a date range, you might include that in the search:

@users = User.joins(:event).where('events.event_date > ? and events.event_date < ?', start_date, end_date)

Next in the view, you aren't consistent with your variable naming. The controller sets up the @users variable, which you access once, but then later you are missing the @ in front of it, which is not the same thing. I don't know what the calendar part wants as input, but at least the for loop should be:

for user in @users

That said, for loops are not very rubyish. The ruby way is to use each:

@users.each do |user|
  ...
end

or even better, to make all of your links:

<ul>
<%= @users.collect {|user| content_tag(:li, link_to h(user.name), user).join } %>
</ul>

edit

Based on more information, I think you are starting at the wrong place. Let's start with events.

Event.joins(:users).where('events.event_date is not null')


<% calendar_for @events, :year => @date.year, :month => @date.month do |calendar| %>
  <%= calendar.head('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',   'Saturday') %>
  <% calendar.day(:day_method => :event_date) do |date, events| %>
    <%= date.day %>
    <ul>
      <% for event in events %>
        <li><%= link_to h(event.user.name), event.user %></li>
      <% end %>
    </ul>
  <% end %>
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文