如何对多个资源使用相同的部分?
How to use same Partial for multiple resources?
我正在使用 tables 列出索引模板中从数据库中记录的内容,并且像往常一样,最后三个 table 单元格用于 Show、Edit 的 links并销毁对象。
../users/index.html.erb
<table>
...
<% @users.each do |user| %>
<tr>
...
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
无论如何,我试图将那些 link 的文本替换为一些 Bootstrap 的字形图标,我成功了,但它变得混乱所以我认为最好将它放在一个部分与使用相同 table 布局的所有索引模板共享的变量。
../shared/_editLinks.html.erb
<td>
<%= link_to dist do %>
<span class="glyphicon glyphicon-eye-open"></span>
<% end %>
</td>
<td>
<%= link_to send("edit_#{dist}_path(#{dist})") do %>
<span class="glyphicon glyphicon-edit"></span>
<% end %>
</td>
<td>
<%= link_to dist, method: :delete, data: { confirm: 'Are you sure?' } do %>
<span class="glyphicon glyphicon-remove"></span>
<% end %>
</td>
然后使用以下代码行在索引 table 中呈现部分内容。将资源名称作为变量传递。
<%= render 'editLinks', dist: user %>
然后第一个和最后一个 links 似乎工作正常但我在中间遇到了这个错误 -Edit- link.
undefined method `edit_user_path(#<User:0x007f611ab015a8>)' for #<#<Class:0x007f611a7064d0>:0x007f611ab143b0>
你能告诉我导致这个错误的原因以及如何让它工作吗?
将参数传递给 route/method 以逗号分隔
<%= link_to send("edit_#{dist.class.name.underscore}_path", dist) do %>
send
方法语法
导致错误的行是因为您试图将对象视为字符串。
因为 _path
助手通常是 snake_case
,您可以像这样在对象的 class 名称上使用 underscore
方法:
<%= link_to send("edit_#{dist.class.name.underscore}_path", dist) do %>
正如 Deepak 所指出的,您还可以提供 dist
对象作为 send
的第二个参数。否则,您最终会遇到类似的错误,因为您将再次将该对象视为可以强制转换为字符串的值。
我正在使用 tables 列出索引模板中从数据库中记录的内容,并且像往常一样,最后三个 table 单元格用于 Show、Edit 的 links并销毁对象。
../users/index.html.erb
<table>
...
<% @users.each do |user| %>
<tr>
...
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
无论如何,我试图将那些 link 的文本替换为一些 Bootstrap 的字形图标,我成功了,但它变得混乱所以我认为最好将它放在一个部分与使用相同 table 布局的所有索引模板共享的变量。
../shared/_editLinks.html.erb
<td>
<%= link_to dist do %>
<span class="glyphicon glyphicon-eye-open"></span>
<% end %>
</td>
<td>
<%= link_to send("edit_#{dist}_path(#{dist})") do %>
<span class="glyphicon glyphicon-edit"></span>
<% end %>
</td>
<td>
<%= link_to dist, method: :delete, data: { confirm: 'Are you sure?' } do %>
<span class="glyphicon glyphicon-remove"></span>
<% end %>
</td>
然后使用以下代码行在索引 table 中呈现部分内容。将资源名称作为变量传递。
<%= render 'editLinks', dist: user %>
然后第一个和最后一个 links 似乎工作正常但我在中间遇到了这个错误 -Edit- link.
undefined method `edit_user_path(#<User:0x007f611ab015a8>)' for #<#<Class:0x007f611a7064d0>:0x007f611ab143b0>
你能告诉我导致这个错误的原因以及如何让它工作吗?
将参数传递给 route/method 以逗号分隔
<%= link_to send("edit_#{dist.class.name.underscore}_path", dist) do %>
send
方法语法
导致错误的行是因为您试图将对象视为字符串。
因为 _path
助手通常是 snake_case
,您可以像这样在对象的 class 名称上使用 underscore
方法:
<%= link_to send("edit_#{dist.class.name.underscore}_path", dist) do %>
正如 Deepak 所指出的,您还可以提供 dist
对象作为 send
的第二个参数。否则,您最终会遇到类似的错误,因为您将再次将该对象视为可以强制转换为字符串的值。