Form is submitting when the page loads

Posted by RailAddict on Stack Overflow See other posts from Stack Overflow or by RailAddict
Published on 2010-03-12T10:21:54Z Indexed on 2010/03/12 10:27 UTC
Read the original article Hit count: 444

Filed under:

I have a really simple Rails app. Basically an article with comments. I want the article page to show the article, comments underneath and then a textbox to write a comment and a submit button to submit it.

I got it all working except for one (big) problem. When the page loads.. example.com/article/1 a blank comment is submitted to the database.

I fixed that by including "validates_presence_of :body" in the Comment model. But that results in the following image when the page loads:

alt text

This is my code by the way:

def show
@place = Article.find(params[:id])
@comment = Article.find(params[:id]).comments.create(params[:comment])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @article }
end
  end

and

    <% form_for([@article, @comment]) do |f| %>
  <p>
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </p>
  <p>
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </p>

  <p>
    <%= f.submit "Create" %>
  </p>
<% end %>

© Stack Overflow or respective owner

Related posts about ruby-on-rails