将表单变量传递给部分模板 - Rails 4

jef*_*sch 2 ruby-on-rails ruby-on-rails-4

我有一个使用semantic_form_for标记的编辑页面

<%= semantic_form_for @photog do |f| %>
Run Code Online (Sandbox Code Playgroud)

表单变得越来越长,我想将其分解为多个部分,以便视图更具可读性.

我正在尝试在edit.html.erb模板中创建部分内容,例如

  <%= render 'favorite_things', locals: {f: f} %>
Run Code Online (Sandbox Code Playgroud)

这段代码打破了未定义的局部变量或方法`f'##:#0x007ff5a110abf0>

我需要了解如何传递f对象,以便部分可以读取它和f.input之类的东西

 <!-- favorite things partial -->
 <div class='edit_photog_header'>A List Of Your Favorites</div>
 <%= f.input :fav_lens, :label => "Lens" %>
 <%= f.input :fav_camera, :label => "Camera" %>
 [and so on]
Run Code Online (Sandbox Code Playgroud)

谢谢

SHS*_*SHS 7

locals适合在你使用的时候render partial.

<%= render partial: path_to_partial, locals: locals_hash %>
Run Code Online (Sandbox Code Playgroud)

如果你刚刚使用render,你应该这样做:

<%= render path_to_view, locals_hash %>
Run Code Online (Sandbox Code Playgroud)

在你的情况下将是:

<%= render 'favorite_things', f: f %>
Run Code Online (Sandbox Code Playgroud)