English 中文(简体)
Ruby on Rails 嵌套块
原标题:Ruby on Rails Nesting Blocks

I have a block that I need to nest another block in. Or maybe I don t and I am doing this totally wrong. There is a UNIT and the UNIT has_many COMPANIES Then COMPANIES has_many USERS

在“UNIT展示页面”上,我尝试做这样的事情:

<% @company.each do |c|%>
 <%= c.name %>
  <% ??? each do |f| %>
   <p>
   Name: <%= f.name %>
      </p>
<% end %>
<% end %>

基本上,对于每个公司,我显示公司的名称,这没有问题,但我试图显示属于该公司的特定用户。我无法在控制器中定义它,因为有多个公司。

That user is: cc = @user.find(:all, :conditions => ["position = ?", "Company Commander"])

我不确定如何通过每个公司进行循环,然后针对该公司进行循环并列出该公司的特定用户。

如果我解释得不好,对不起。我不确定这是否是正确的做法。

提前感谢。

最佳回答

看起来您想列出每个公司与其相关联的“公司指挥官”职位的用户。

或许在用户上创建一个命名范围会有帮助:

class User < ActiveRecord::Base
  ...
  named_scope :company_commanders, :conditions => "position =  Company Commander "
end

现在你可以按照自己的需求循环遍历它们。

<% @company.each do |c|%>
 <%= c.name %>
 <% c.users.company_commanders each do |f| %>
   <p>
     Name: <%= f.name %>
   </p>
 <% end %>
<% end %>
问题回答

试试这个:

<% @company.each do |c| %>
  <%= h(c.name) %>
  <% c.users.each do |u| %>
    <p>Name: <%= h(u.name) %></p>
  <% end %>
<% end %>

首先,您需要在您的模型中定义关系:

# Unit model
has_many :companies
has_many :users, :through => :companies

# Company model
has_many :users
belongs_to :unit

...

然后在您的单元控制器中:

@unit = Unit.find(params[:id]) # or something similar

在你的观点中:

<% @unit.companies.each do |c|%>
 <%= c.name %>
  <% c.users each do |f| %>
   <p>
     Name: <%= f.name %>
   </p>
  <% end %>
<% end %>

如果一个公司有很多用户,那么你应该能够只使用 company.users 来获取与该公司相关联的用户列表,然后你可以按照你喜欢的方式循环遍历或列出这些用户。

假设您在模型中设置了关系,这只是活动记录为您设置这些关联的方法。

这就是最终工作的结果:

named_scope :company_commanders, :conditions =>{:position =>  Company Commander }

<% @company.each do |c| %>
  <%= h(c.name) %><br />
  <% if c.users.company_commanders.blank? %>
    <%= link_to "Add this User", new_user_path %><br />
  <% else %>
    <% c.users.company_commanders.each do |u| %>
        <p>Name: <%= h(u.name) %></p>
    <% end %>
  <% end %>
<% end %>
</p>




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签