michaelreeves
michaelreeves

Reputation: 323

Yielding in Rails partials

I'm using Rails 4.2, but following along with the Rails 5.0 documentation on partials, trying to build a search partial. http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

I am getting the Search button, so the shared partial is rendering, however, it does not seem to be yielding back to the calling block. I'm not getting the label or the search_field. Did I do something dumb, or does this not function in Rails 4.2? Seems like this should just be basic Ruby.

I did notice that this example is not included in the docs for 4.2. http://guides.rubyonrails.org/v4.2/layouts_and_rendering.html#using-partials

shared/_search_filters.html.erb

<div class="search">
  <%= search_form_for @q, html: { class: 'form-inline'} do |f| %>
    <div class="form-group">
      <%= yield f %>
    </div>
    <%= f.submit 'Search', class: 'btn btn-default', role: 'button' %>
  <% end %>
</div> <!-- /.search -->

index.html.erb

<%= render 'shared/search_filters', search: @q do |f| %>
  <%= f.label :search, 'Search (optional)', class: 'control-label'  %>
  <%= f.search_field :name_or_description_cont,
                     class: 'form-control',
                     placeholder: 'Search (optional)' %>
<% end %>

Upvotes: 2

Views: 2289

Answers (2)

Derek Bruneau
Derek Bruneau

Reputation: 76

It looks like Rails only supports yielding to blocks in partials if you're using 5.0 or later. From the release notes for 5.0:

Allow blocks while rendering with the render partial: helper.

Upvotes: 2

Pedro H. Grandin
Pedro H. Grandin

Reputation: 1

Try replace this at your index:

 render layout: 'shared/search_filters', locals: { search: @q } do |f|

Upvotes: 0

Related Questions