0

I have the following bit of code in my views:

- if admin?
  .meta
    Administrator options: 
    = link_to 'Edit This Post', edit_post_path(@post)
    |
    = link_to 'Delete This Post', @post, :method => 'delete', :confirm => 'Are you sure?'

I find that I use this same basic snippet a lot, but sometimes with different resources, and sometimes with more than one resource on a page. I'd like to extract this into a partial to DRY it up, but I need to write is so it could work with local or instance variables for any model. For instance, I need it to work with:

@post, post, @page, page

How do you do that?

Andrew
  • 42,517
  • 51
  • 181
  • 281
  • write a partial and pass parameters to it, will that not solve your problem? just google for passing parameters and you should be fine – aishwarya Nov 18 '11 at 18:34
  • The real problem is the route helpers, how do you make a generic "edit this" link without knowing the model name? – Andrew Nov 18 '11 at 19:20

2 Answers2

1

You can commit a variable to a partial using the :object or the :collection options in the render method. The :object option passes a single object to the partial that can contain anything. The :collection option is used to pass an Array of Objects that must be from the same Class.

For example you have your @post and pass it to the partial like this

<%= render :partial => 'layouts/my_partial', :object => @post, :as => :my_local_var %>

Then you can access the object by calling my_local_var in the partial. Using :collection is slightly different but you can read it on your own here: http://guides.rubyonrails.org/layouts_and_rendering.html

A Problem will be the usage of one partial in different Classes because of the routes names. Then you cant use the edit_modelname_path(@instance_var) you must use the routes Hash { :controller => params[:controller], :action => 'edit' } in the link_to method!

// For Link creation wihout params[:controller] :

Its just an Idea I didnt do this before but you can try this for example:

You render the partial and use :object => @instancevar, :as => :my_local_var you can use the following line for dynamic link creation:

<%= link_to "link text", { :controller => my_local_var.class.to_s.pluralize.downcase, :action => 'edit', :id => my_local_var.id } %>

Then you dont depend on the params[:controller] but are able to link to the vars edit link.

davidb
  • 8,884
  • 4
  • 36
  • 72
  • This is helpful, thanks! Can you think of a way to create an edit link without depending on the controller param? I might like to have this kind of link on the home page for multiple different models on the same screen, none of which would correspond to the controller currently rending the view. If you have an idea how to infer the edit link from a given model instance, that would be great! – Andrew Nov 18 '11 at 19:22
1

You can cleanly write your partial to be independent of the given object:

- if admin?
  .meta
    Administrator options: 
    = link_to 'Edit', [:edit, object]
    |
    = link_to 'Delete', object, :method => 'delete', :confirm => 'Are you sure?'

And then you could do something like

render :partial => 'shared/admin_link', :locals => { :object => @post }
nathanvda
  • 49,707
  • 13
  • 117
  • 139
  • `= link_to 'Edit', [:edit, object]` Brilliant! Is this documented somewhere? This is exactly what I've been searching for. – Andrew Nov 19 '11 at 16:41
  • I can no longer find any "official" documentation of it anymore, but [here](http://apidock.com/rails/ActionView/Helpers/UrlHelper/url_for) it is mentioned in the comments. – nathanvda Nov 19 '11 at 16:57