ruby on rails - How to use same Partial for multiple resources? -
i'm using tables list recorded database in index templates, , usual last 3 table cells used links of show, edit , destroy object.
../users/index.html.erb
<table> ... <% @users.each |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 sure?' } %></td> </tr> <% end %> </tbody> </table>
anyway, trying replace links text bootstrap's glyph icons, , succeeded got messy thought better put in partial variable shared index templates use same table layout.
../shared/_editlinks.html.erb
<td> <%= link_to dist %> <span class="glyphicon glyphicon-eye-open"></span> <% end %> </td> <td> <%= link_to send("edit_#{dist}_path(#{dist})") %> <span class="glyphicon glyphicon-edit"></span> <% end %> </td> <td> <%= link_to dist, method: :delete, data: { confirm: 'are sure?' } %> <span class="glyphicon glyphicon-remove"></span> <% end %> </td>
then use following code line render partial in index table. passing resource name variable.
<%= render 'editlinks', dist: user %>
then first , last links seems work fine got error around middle -edit- link.
undefined method `edit_user_path(#<user:0x007f611ab015a8>)' #<#<class:0x007f611a7064d0>:0x007f611ab143b0>
can tell me causes error , how work?
the lines causing errors because you're trying treat object string.
because _path
helpers typically snake_case
, can use underscore
method on object's class name so:
<%= link_to send("edit_#{dist.class.name.underscore}_path", dist) %>
as pointed out deepak, can providing dist
object second argument send
. otherwise, you'll end similar error because you'd again treating object value can coerced string.
Comments
Post a Comment