如何在rails中的另一个控制器动作中显示一个控制器显示动作

时间:2017-01-24 13:10:28

标签: ruby-on-rails ruby-on-rails-4

我正在使用两个控制器事件和incident_list,我在events_list / index页面中显示所有事件,当我在每个事件中单击show button时我想显示该事件详细信息

IncidentListController

def show
   @incidents = Incident.all
   @incident = Incident.find(params[:id])
   render "incidents/show"
 end

IncidentController

def show
  @incidents = Incident.all
  @incident = Incident.find(params[:id])
 end

incidnet_lists / index.html.erb

<table>
  <table class="table table-striped table-bordered">
   <thead >
  <tr>
      <th class="c2">Case ID</th>
      <th class="c2">Call Type</th>
      <th class="c2">Number of People</th>
      <th class="c2"></th>

  </tr>
 </thead>
<tbody>
  <tr>
    <td><%= inc.id %> </td>
    <td><%= inc.call_type %> </td>
    <td><%= inc.no_of_people %></td>
    <td><%= link_to 'Show', @incidents %></td>

  </tr>
 </tbody>
</table> 

事件/ show.html.erb

 <table class="table table-striped table-bordered">
   <thead>
     <tr>
       <td class= "c2" > Case Id </td>
       <td class= "c2"> Caller Name </td>
       <td class="c2"> Contact Number </td>
       <td class= "c2"> Calling For </td>
       <td class="c2"> Call Type </td>
      <tr>
      </thead>
     <tbody>
       <% @incidents.each do |inc| %>
       <tr>
         <td> <%= inc.id %> </td>
         <td> <%= inc.caller_name %></td>
         <td> <%= inc.contact_number %></td>
         <td> <%= inc.for_whom %> </td>
         <td> <%= inc.call_type %> </td>
       </tr>
       <% end %>
     </tbody>
   </table>

当点击events_list页面中的show按钮时,应该显示事件显示页面

1 个答案:

答案 0 :(得分:1)

REST方法完全涵盖了您的情况。

根据该方法,您应该拥有单个IncidentsController控制器。那你应该

  1. show操作会将show.html.erb视图中的特定事件公开为@incident实例变量
  2. index操作会将所有(或某些)事件的列表公开为index.html.erb视图@incidents实例变量。
  3. 所以你不需要也不应该从另一个控制器的动作中调用一些控制器的动作。

    这是“Rails方式”,它是“REST方式”。这就是人们应该如何做到的。

    您遇到的最简单的提示是为事件资源生成rails scaffold,并正确使用生成的文件进行设置。

    rails generate scaffold Incident
    
相关问题