2

So, I have this code in a partial called "_form.html.erb" in "app/views/posts":

<%= form_for @post do |f| %>
    <% if @post.errors.any? %>
        <h2>Errores:</h2>   
        <ul>
            <% @post.errors.full_messages.each do |message| %>
                <li><%= message %></li>
            <% end %>
        </ul>
    <% end %>
<p>
    <%= f.label :title %><br />
    <%= f.text_field :title %><br />
<br />
    <%= f.label :content %><br />
    <%= f.text_area :content %>
</p>

And I have two views with this code:

"app/views/posts/new.html.erb"

<h1>Nuevo post</h1>

    <%= render 'form' %>

<p>
    <%= f.submit "Agregar Nuevo Post" %>
</p>
<% end %>

"app/views/posts/edit.html.erb"

<h1>Editar Post</h1>

    <%= render 'form' %>

<p>
    <%= f.submit "Actualizar Post" %>
</p>
<% end %>

What I want is the form to render in the views, but with the code I have shown to you I'm getting this error output in the server:

SyntaxError in Posts#new

Showing /home/levick/rubyblog/app/views/posts/new.html.erb where line #10 raised:

/home/levick/rubyblog/app/views/posts/new.html.erb:10: syntax error, unexpected keyword_ensure, expecting $end
Extracted source (around line #10):

7: </p>
8: <% end %>
Trace of template inclusion: app/views/posts/new.html.erb

Rails.root: /home/levick/rubyblog

Application Trace | Framework Trace | Full Trace
Request

Parameters:

None
Show session dump

Show env dump

Response

Headers:

None

The same with the edit view.

My Question is: What did I do wrong?

Thanks!

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
Zauu
  • 55
  • 5

3 Answers3

1

You cannot use the form variable outside of the partial. In both your new and edit pages you reuse the f variable even though it only exists inside the partial.

I would recommend you fix the error by moving the creation of the submit buttons inside the partial. Don't forget to also move the <%- end %> inside the partial too since you are finishing the form inside the partial.

Jason
  • 3,736
  • 5
  • 33
  • 40
  • But in my edit view The button says something else... How can I keep that behaviour? – Zauu Jun 13 '12 at 02:09
  • You can pass in variable to the partial. See http://stackoverflow.com/a/3222958/136584 for examples. Basically you are setting the :locals hash. – Jason Jun 13 '12 at 02:11
  • Could you please give me a practical example exactly for my case? I tried the examples in the question you link to and didn't work... – Zauu Jun 13 '12 at 02:33
1

Try the following in your partial _form.html.erb

<%= form_for @post do |f| %>
    <% if @post.errors.any? %>
        <h2>Errores:</h2>   
        <ul>
            <% @post.errors.full_messages.each do |message| %>
                <li><%= message %></li>
            <% end %>
        </ul>
    <% end %>
<p>
  <%= f.label :title %><br />
  <%= f.text_field :title %><br />
<br />
  <%= f.label :content %><br />
  <%= f.text_area :content %>
</p>
<%= f.submit @post.new_record? ? 'Nuevo Post' : 'Actualizar Post' %>
<% end %>
Kleber S.
  • 8,110
  • 6
  • 43
  • 69
  • Sorry, my mistake. Just forgot an `?` in the `submit` line, see the updated code above. – Kleber S. Jun 13 '12 at 02:48
  • weird. try to remove the comma after the submit. – Kleber S. Jun 13 '12 at 02:56
  • Hooray! it works! Now, I've seen you just changed this line: <%= f.submit @post.new_record? ? 'Nuevo Post' : 'Actualizar Post' %> in all the code, Could you explain me why did it work? what have you done exactly? (theorical part) – Zauu Jun 13 '12 at 02:59
  • I've already accepted your answer, could you explain me why did that work? – Zauu Jun 13 '12 at 03:04
  • First, @post will check if where it is coming from, if it is coming from an `new` action, they know that it is an new record and if so, will display `Nuevo Post`, else display `Actualizar Post`, all this in one line making use of ternary operator. – Kleber S. Jun 13 '12 at 03:08
  • I get it! O.o You're Awesome!! Thank You. Didn't know about the ternary operator... Beautiful Solution ;) – Zauu Jun 13 '12 at 03:14
0

You're missing an <% end %> to close your <%= form_for %> block.

Peter Brown
  • 50,956
  • 18
  • 113
  • 146