1

I'm following this tutorial - http://tutorials.jumpstartlab.com/projects/blogger.html#i2:-adding-comments. It is building a sample blog app and so far has an Article model and is adding a Comment model. An Article has_many Comments.

They want to be able to create the comment using a form on the articles page:

<h3>Post a Comment</h3>

<%= form_for [ @article, @comment ] do |f| %>
  <p>
    <%= f.label :author_name %><br/>
    <%= f.text_field :author_name %>
  </p>
  <p>
    <%= f.label :body %><br/>
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit 'Submit' %>
  </p>
<% end %>

I don't understand how this form works though. It binds two objects to the form, and it says that it'll submit to article_comments_path, which will POST to comments#create.

How does this work? How does it know what to do when it has two objects? Does the order of the objects in the array matter? Can you have more than two objects?

1 Answer 1

1

you are probably going to use nested resources:

resources :articles do
   resources :comments
end

[ @article, @comment ] is an array which will build a nested route. Like:

 /articles/1/comments

article_comments_path maps to the index action (GET) and to create action(POST)

dorake routes to see what i'm talking about:

article_comments_path    GET     /articles/:article_id/comments(.:format)    comments#index

                         POST    /articles/:article_id/comments(.:format)    comments#create

So essentially, you use [ @article, @comment ] when you have nested resources to create routes helpers. the order of the objects matter as it is article_comments_path and NOT comment_articles_path.

whether you can bind more than 2 objects to the form, it depends, I think, on how deeply nested are your resources

UPDATE:

you are using a form. and forms are for either to create or update objects depending on whether the object exists or not. The submit button will trigger that. This article sums it pretty good

"When the user clicks submit on a form, Rails invokes either the create (for a new record) or update (for an existing record) action in the associated controller. Forms are, by default, submitted with an HTTP POST command, and all the information that was entered in the form is provided in a hash called params, which is available to the controller."

Sign up to request clarification or add additional context in comments.

3 Comments

I know that it's doing these things, but I don't know how/why. So I could copy the code, but I wouldn't be able to expand off of it. Like I wouldn't know how to delete a comment or edit it or something. I think what I'm missing is how you go from the array to the action of the form.
Ok, so 1) I wouldn't be using a form to delete, and 2) it'll know whether to create or update based on the objects that are passed in?
yes. you don't use a form to delete. let's say you are in the show page (articles/1) and reading article one. and at the bottom of the article you have delete button. when you click delete, it will go to the controller's destroy action. it knows to go there because next to the delete you specified the path to the destroy action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.