Search code examples
ruby-on-railspartial-viewsrenderpartial

Render partial with different link_to paths


I am trying to render a partial that displays a list of names (that I created using :resource :create) and link_to their respective pages. However, I want to implement this twice such that one list links to the show pages and the other links to their edit pages.

I would prefer to use one partial to accomplish this because they are exactly identical except for their url_path.

footer.html.erb:

<!--Show-->
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                <b class="icon-eye-open icon-white"></b>
                                <b class="caret"></b>
                            </a>
                            <ul class="dropdown-menu">
                                <%= render current_user.apps %>
                            </ul>
                        </li>

<!--Edit-->
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                <b class="icon-wrench icon-white"></b>
                                <b class="caret"></b>
                            </a>
                            <ul class="dropdown-menu">
                                <%= render current_user.apps %>
                            </ul>
                        </li>

_app.html.erb:

Show should have app and Edit should have edit_app_path(app) for link_to's second argument

<li>
    <%= link_to app.name, app %>
</li>

Solution

  • If your partial contains only

        <li>
          <%= link_to app.name, app %>
        </li>
    

    I would suggest to write it two times as as list without css classes isn't enough for a partial imho.

    But if you really want to use a partial you can do it with a local variable in Rails 3 way:

        <li>
            <%= link_to @app.name, @path %>
        </li>
    

    and

        <%= render "shared/apps", :collection => current_user.apps, :locals => {:path=> @app} %>
        <%= render "shared/apps", :collection => current_user.apps, :locals => {:path=> edit_app_path(app)} %>