0

i want to use a common partial to render a listing page and its going good expect i am facing a problem to generate dynamic link_to for :edit and :delete action which is common.

so i have @menu and @picture model in two different views rendering the same common partial (which i created).

this is my common partial.

shared/_index_common_grid.html.erb

               <% pictures.each do |picture| %>
                  <div class="col-sm-4 pull-left">
                      <div class="thumbnail">
                          <div class="caption"> 
                               <h4><%= picture.title.capitalize%></h4>
                              <p class="text-muted">
                              <!--THIS IS WHERE I WANT TO HANDLE DYNAMIC GENERATION OF LINK_TO FOR @picture and @menu models --> 
                                 <%= link_to "Edit", [:edit, current_user,@request,@shop,picture]%>
                              |  <%= link_to "Delete", [current_user,@request,@shop,picture],:data=>{:confirm=>"Are you sure ?"}%>
                              | <span class="pull-right"><%= show_formatted_date(picture.created_at)%></span>                                
                              </p>
                          </div>
                      </div>
                  </div> 
                <%end%>

this is my one view using above common partial,there is one more similar to this except i pass different model.

##my view page --------pictures/index
                  <%unless @pictures.blank?%>   
                      <%= render partial: "shared/index_common_grid", locals: {pictures: @pictures}%>
                  <%end%>

I dont want to go with switch case in application_helper which can be done easily.

3
  • So you are using this partial to list the pictures and menus? Just the issue is link for edit and delete for pictures and menus will be different? What are the required params for the menu? And are you on rails 3 or rails 4? Commented Dec 30, 2016 at 7:54
  • its Rails 4(mentioned in the question :0).Yeah..i want dynamic links to each resource... Commented Dec 30, 2016 at 9:34
  • So what is the link for the menus? Means there are options like url_for in rails which might help you. But that depends on the parameters required for each of them. Commented Dec 30, 2016 at 10:18

1 Answer 1

2

You can use layouts with your partials. See the API docs for more details.

Rendering pictures with layouts

First, rewrite pictures/index.html.erb to:

<%= render partial: 'picture', layout: 'shared/index_common_grid', collection: @pictures, as: :object %>

Second, rewrite shared/index_common_grid.html.erb to:

<div class="col-sm-4 pull-left">
  <div class="thumbnail">
    <div class="caption"> 
      <h4><%= object.title.capitalize%></h4>
      <p class="text-muted">
      <%= yield %> | <span class="pull-right"><%= show_formatted_date(object.created_at)%></span>                                
      </p>
    </div>
  </div>
</div> 

Thrid, create pictures/_picture.html.erb:

<%= link_to "Edit", [:edit, current_user, @request, @shop, picture]%>
|  <%= link_to "Delete", [current_user, @request, @shop, picture], :data=>{:confirm=>"Are you sure ?"}%>

Adding other types of objects

If you want to render other types of objects (menus in your example) then:

  1. Add menus/index.html.erb with a content similar to pictures/index.html.erb (replace picture and @pictures with menu and menus).
  2. Add menus/_menu.html.erb with links rendered appropriately for menus.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Greg...this is good approach but i am looking furthermore for a way to handle all objects by just one CONCERN method...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.