[英]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 那你应该有
show
action which would expose particular incident to show.html.erb
view as @incident
instance variable show
将特定事件作为@incident
实例变量暴露给show.html.erb
视图的@incident
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.