简体   繁体   English

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

[英]how to display one controller show action in another controller action in rails

I am using two controller incident and incident_list, i am displaying all incidents in incident_list/index page when click on show button in each incident i want to display that incident details 我正在使用两个控制器identification和incident_list,在每个事件中单击“显示”按钮时,我要在event_list / index页中显示所有事件,我想显示该事件的详细信息

IncidentListController IncidentListController

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

IncidentController IncidentController

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

incidnet_lists/index.html.erb 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> 

incidents/show.html.erb events / 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>

when click on show button in the incident_list page incident show page should be display 当单击identity_list页面中的显示按钮时,应显示事件显示页面

Your situation is perfectly covered by REST approach. REST方法完全可以解决您的情况。

According to that approach you should have the single IncidentsController controller. 根据该方法,您应该具有单个IncidentsController控制器。 Then you should have 那你应该有

  1. show action which would expose particular incident to show.html.erb view as @incident instance variable show将特定事件作为@incident实例变量暴露给show.html.erb视图的@incident
  2. index action which would expose the list of all (or some) incidents to index.html.erb view as @incidents instance variable. index操作,它将所有(或某些)事件的列表作为@incidents实例变量暴露给index.html.erb视图。

So you don't need and should not call some controller's action from another controller's action. 因此,您不需要并且不应从另一个控制器的动作中调用某个控制器的动作。

It's the "Rails Way", it's the "REST Way". 这是“ Rails Way”,这是“ REST Way”。 It's the way how one should do that. 这就是应该如何做的方式。

The simplest tip in your situation is generate rails scaffold for your incidents resources and use the generated files properly to set up things. 在这种情况下,最简单的提示是为事件资源生成Rails脚手架,并正确使用生成的文件进行设置。

rails generate scaffold Incident

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将一个控制器动作映射到另一个动作导轨上 - Map one controller action on another action rails Rails 3-与Kaminari分页-显示一个控制器的动作,浏览另一个控制器的记录 - Rails 3 - Pagination with Kaminari - Show action of one controller, to browse records of another 从一个控制器调用Rails 5动作 - Calling a Rails 5 action from one controller to another Rails - 如何避免必须从另一个控制器中的create action触发一个控制器中的create操作 - Rails - how do I avoid having to trigger the create action in one controller from the create action in another controller Rails:如何在同一个控制器中将值从一个动作传递到另一个动作 - Rails: How to pass value from one action to another in the same controller 如何在Rails控制器中的show action中获取值 - How to get values in show action in rails controller 如何在 Rails 中访问 URL 在 Controller 中显示操作 - How to access URL in Rails Show action in Controller Rails:如何在内部POST到另一个控制器动作? - Rails: How to POST internally to another controller action? 在另一个控制器中重定向到SHOW操作 - Redirect to SHOW action in another controller Rails:从控制器调用另一个控制器动作 - Rails: call another controller action from a controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM