0

Rails 3.2.13 & ERB

I am trying to get send some link_to items to a partial. I am sending in a title to the partial successfully as below.

<%= render :partial =>'form', 
       :locals => {:page_title => t(:'<h2>Editing Feature</h2>')}

What I dont like is that I am currently doing this:

<%= link_to 'Show', @feature %> |
<%= link_to 'Back', features_path %>

as part of the primary page. I would like to take this codeblock and send it to the partial for rendering there. The primary reason is the simple_form is defined in the partial and I have a well that contains everything on that page. Except for the two link_to items down on the bottom of the page. If I could pass them into the partial somehow (I assume as a code block) then I can decide where to place them and how to render them in the form itself instead of kind of as an afterthought.

Thanks.

1 Answers1

0

Typically we use partials to break off HTML chunks into separate modular components and reduce repetition. Partials have the same variable context as the template that rendered them, so as-is, you wouldn't have any trouble simply relocating your links to your partial and everything should just work.

I think the better practice, however, would be to pass your instance variable as a local to reduce coupling with your controller. Something like:

Your view:

<%= render :partial =>'form', 
           :locals => {:page_title => t(:'<h2>Editing Feature</h2>'), 
           :feature => @feature} %>

_form.html.erb:

<%= link_to 'Show', feature %> |
<%= link_to 'Back', features_path %>

This way, if you were to render the partial elsewhere in your application you don't need to have an instance variable handy. An example situation where this could be useful would be looping through multiple "features" (a la an index view) and spitting out the relevant HTML as defined in the partial.

Noz
  • 6,216
  • 3
  • 47
  • 82