Search code examples
rubyruby-on-rails-4blockpartials

Separating Ruby Code which requires a block with Partials


I want to use

<% form_tag ( <<variables from partial here>> ) do %>

on a partial; problem is on the IDE it already is trying to tell me I have invalid code; this is because the "DO" is to be paired with an "END". I cannot end on the partial itself; because there is some content after the "DO" which is on the page where the partial is called.

How do I use the form_tag in the partial while using the content I have from the page which called the partial?


Solution

  • Sounds like you're in a very sticky situation. This is what I think you've got from your description:

    #partial_1
    <% form_tag (:model_name) do %>
    
    #partial_2
    <% form_contents %>
    <% end %>
    
    #controller
    @model = Model.load
    render(partial1 << partial2)
    

    You may have to correct me if I'm wrong, but is it possible instead to do this?

    #controller
    @model = Model.load
    render(partial_1)
    
    #partial_1
    <% form_tag ... %>
    <%  render(partial_2) %>
    <% end %>
    
    #partial_2
    <% all the form guts %>
    

    If you're using straight ruby you're probably using the ERB library and you are binding every time, which should keep the variables live all the way through.