Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

form_forの中でrenderを使いたい場合localオプションで指定する

form_forの中でrenderを使いたい場合、localオプションで指定します。

まず、大元のページで

<!-- _form.html.erb -->
<%= form_for(@picture) do |f| %>

  <!-- 中略 -->

  <div class="field">
    <%= render :partial => "page_1", :locals=>{:f=>f} %>
  </div>

  <!-- 中略 -->

<% end %>

こうすれば、render先のファイルでもform_forのブロック変数*1を使い回せます。・

<!-- _page_1.html.erb -->
<%= f.label :image %>
</br>
<%= f.file_field :image %>

*1:...で良いのでしょうか...