Gunther Gib
Gunther Gib

Reputation: 117

Why isn't this provide and yield not working

Trying to pass these into the form.

<% provide(:title,"Edit Type") %>
<% provide(:content,"Edit Type") %>
<% provide(:button,"Update") %>
<% provide(:link,"form_for(@type, url: product_type_path, method: :patch)") %>  <!-- edit for product_type model(@type) url: go to product_type controller with update action -->
<%= render 'form' %>

_form.html.erb

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= yield(:link) do |f| %>  
        <h3><%= yield(:content) %></h3>
        <%= render 'shared/error_messages', object: f.object %>

        <%= f.label :name %>
        <%= f.text_field :name, class: 'form-control' %>

        <%= f.label :value %>
        <%= f.number_field :value, class: 'form-control' %>

        <%= f.submit yield(:button), class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

The error the is generated is either implicit symbol conversion to integer or something related to end of keyword ensure.

What is wrong in this.

Upvotes: 0

Views: 257

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

This complex helper under :link isn't going to work as you expect...

<% provide(:link,"form_for(@type, url: product_type_path, method: :patch)")

All this will do is render the following string in your view...

form_for(@type, url: product_type_path, method: :patch)

You can't even do <%= eval(yield :link) %> because you don't have a block, and if you modified your string to include do |f| it wouldn't recognise it as the start of a block anyway.

Generally, I think what you want to do is impossible, and even if you could make it work, it would be a really bad idea in terms of readability, and possibly security.

The provide and content_for are a nice way to store and deliver simple markup to various views, but the markup has to be self contained, and can't be (for example) the beginning of a block.

You could possibly use it to store the raw <form ... > information, but I can't see it being used with the form_for helper.

Upvotes: 1

Related Questions