0

I am building a multi step form, and I'd like to reuse the button code across multiple views. here is the code I'd like to break out into a partial layout:

<div class="actions">
<%= link_to "Back", previous_wizard_path, :class => 'btn btn-large' %>
<%= f.submit "Next", class: "btn btn-large btn-primary" %>
</div>

I have copied this code into a new partial layout called "_action_buttons.html.erb".

I am trying to render this partial with the following code:

<%= render 'multi_form/action_buttons' %>

However, when I try to run this, I get the following error due to the f.submit:

undefined local variable or method `f' for #<#<Class:0x007fbae16c1dc8>:0x007fbae13b33e8>

Extracted source (around line #3):

1: <div class="actions">
2:     <%= link_to "Back", previous_wizard_path, :class => 'btn btn-large' %>
3:     <%= f.submit "Next", class: "btn btn-large btn-primary" %>
4:   </div>

how can I modify my render call so that this error doesn't happen? I realize this is probably a common issue; help a noob out!

diego
  • 123
  • 2
  • 14

2 Answers2

2
  1. You should not be breaking the form tag as it may cause some unexpected behavior, and it break the readability of your code.
  2. If you really want to follow this approach, one technique is you can pass the form variable into your template, you can try to do as the following:

    <%= render :partial => 'multi_form/action_buttons', :locals => {:f => f} %>
    
Sang Do
  • 91
  • 1
  • 7
  • thanks for this. I see your point, but my objective is to keep the code DRY. I have the exact same button code across many views for this multi-step form. Do you have any thoughts on other ways this could be achieved without breaking the form tag? – diego Oct 19 '12 at 13:25
  • 1
    It's not bad to have a submit button that is similar to other forms. Here are a couple more things you can improve: 1. you can group the submit styling for all the submit buttons; 2. you can create a "generic form", then pass different params in to generate the form; 3. I see you use ERB engine, try Haml, I think it's cleaner and easier to code. – Sang Do Oct 25 '12 at 16:33
1

You can use the more concise form without the keyword :partial.

Check this question Rails: confused about syntax for passing locals to partials

Community
  • 1
  • 1