显示指定==“经理”列的数据

时间:2017-03-23 12:36:08

标签: ruby-on-rails git

我有三个模型如下。检查我是否正确关联它们.sorry.am new to rails

  class Designation < ActiveRecord::Base
     has_many :employes
  end


  class Employe < ActiveRecord::Base
     belongs_to :designation
     has_many :branch
  end



  class Branch < ActiveRecord::Base
    belongs_to :employe
  end

在我的员工表格中,我添加了一个选择框以选择名称。并在雇员索引页面中显示...现在问题是我想显示在我的分支模型中具有指定为经理的雇员的姓名。怎么做..... 这是我的分公司控制器

    def index
      @branches = Branch.all
    end

    def show
    end

    def new
    @branch = Branch.new
    end

这是我的分支视图

      <table id="example1" class="table table-bordered table-striped">
                <thead>
                      <tr>
                        <th>Branch name</th>
                        <th>Branch address</th>
                        <th>Phoneno</th>
                         <th>Mangers name</th>
                         <th>Status</th>                                 
                        <th>Action</th>
                        </tr>
                    </thead>
                          <tbody>
                                <% @branches.each do |branch| %>
                              <tr>

                                    <td><%= branch.branch_name %></td>
                                    <td><%= branch.branch_address %></td>
                                    <td><%= branch.phoneno %></td>
                                    <td><%= branch.employe_id %></td>

                                      </div>
                                      <td></td>
                                      <td>
                  <% if branch.is_active == true %>
                <%= link_to " Active ",edit_branch_path(branch),class:   
                   "btn btn-warning"%>
                <%else%><%= link_to
                      "Inactive",edit_branch_path(branch),class: "btn 
                      btn-warning"%>
                  <%end%>

                   </td>
                   <td> 
             <div class="btn-group">
            <span class=" btn btn-info dropdown-toggle"
             data-toggle="dropdown" href="#menu1">Action
            <b class="caret"></b>
              </span>

            <ul class="dropdown-menu">
          <li><%= link_to 'Edit', edit_branch_path(branch) %></li>
         <li><%= link_to 'Destroy', branch, method: :delete, data: {
           confirm: 'Are you sure?' } %></li>
           </ul>
          </li> 
           </td>
        </div>
       </tr>
       <% end %>
        </tbody>
       </table>

2 个答案:

答案 0 :(得分:0)

纠正以下关联:

 class Branch < ActiveRecord::Base
   has_many :employes
 end

 class Employe < ActiveRecord::Base
   has_one :designation
   belongs_to :branch
 end

class Designation < ActiveRecord::Base
  belongs_to :employee
end

的观点

<table id="example1" class="table table-bordered table-striped">
  <thead>
    <tr>
      <th>Branch name</th>
      <th>Branch address</th>
      <th>Phoneno</th>
      <th>Mangers name</th>
      <th>Status</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <% @branches.each do |branch| %>
      <tr>
        <td><%= branch.branch_name %></td>
        <td><%= branch.branch_address %></td>
        <td><%= branch.phoneno %></td>
        <% managers = [] %>
        <% branch.employes.each{|e| managers << e.name if e.designation.name == "manager" } %>
        <td><%= managers.uniq %></td>
      </tr>
    <% end %>
  </tbody>
</table>

答案 1 :(得分:0)

我认为员工应该有一个名称,请检查以下修改过的型号代码,

let image = UIImage("b3")
let imageToShare = [image!]
let shareImage =  UIActivityViewController (activity items:imageToShare,application Activities:nil)
shareImage.popoverPresentationController?.sourceView= self.view
shareImage.excludedActivityTypes = [UIActivityType.print,UIActivityType. cipyToPasteboard]
self.present (shareImage,animated:true,completion:nil)