Rails:视图中两个属性不同的表
我一直在尝试使用来自两个不同模型的两个属性,以便它们可以与索引视图中 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管我不熟悉日历库,但我看到了两个问题。
首先,确保控制器中的查询返回有用的内容。在您给我们的三行中,第一行甚至不搜索,它调用未定义变量的方法。第二个接近工作,但您正在搜索一个日期并将其与 true 匹配...怎么样:
此外,如果您有一个日期范围,您可能会将其包含在搜索中:
接下来在视图中,您是'与您的变量命名一致。控制器设置了 @users 变量,您访问该变量一次,但稍后您会丢失它前面的 @,这不是同一回事。我不知道日历部分想要什么作为输入,但至少 for 循环应该是:
也就是说,for 循环不是很Ruby。 ruby 方法是使用
each
:甚至更好,使所有链接:
edit
根据更多信息,我认为您从错误的地方开始。让我们从事件开始。
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:
Furthermore, if you have a date range, you might include that in the search:
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:
That said, for loops are not very rubyish. The ruby way is to use
each
:or even better, to make all of your links:
edit
Based on more information, I think you are starting at the wrong place. Let's start with events.