Search code examples
ruby-on-railsruby-on-rails-3partial-viewsrenderpartial

how do I break f.submit from form builder into a partial layout in rails 3?


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!


Solution

    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} %>