tags:

views:

671

answers:

2

Forgive the title of the question; spent 20 minutes trying to write something coherent (doubtful if succeeded).

Anyways, using a blog as an example, our models look like this:

Post hasMany Comment Comment belongsTo Post

Let's say I want to add a new comment: when saving the comment, I need to include the foreign key to the parent Post (Post.id) in the data array before calling save.

Because I want to keep the relationship in context to the user -- I don't want them to have to choose a post from a select field to associate the comment with -- here's two options I see:

1) Put the comment form in the Post's view.

2) pass the Post Id as an argument (via GET) to the Comment.add action (or write/read to/from Session); save and redirect back to the parent Post.view.

Option 1 is great for a blog, but not for a User's Profile, which could be too long of a form to include in the User view neatly.

Option 2 works I guess, but I'd have to write some Post-specific display/redirect conditionals to the Comments.add action, and it doesn't seem right; from what I've read, I should be trying to maintain a 1:1 relationship between controllers and models.

Is #2 acceptable as a Web MVC best practice? or is there another option I'm overlooking?

A: 

Both options seem fine to me.

  1. If the form is too long, just stick in it a partial view.
  2. I don't see a problem with using a model in multiple controllers. You shouldn't use every model in every controller, of course, but if you need both models, why not?
mgroves
+1  A: 

I've often done both together. I've put the form in the view for the post (probably using an element, if you want to put it elsewhere as well), but had it submit to the add action of it's controller (comments, in this case) and pass the id of the post. Just a note - if you need to implement private posts, etc, it would be best to have the check in the add action of the Comments controller when it comes in as well.

michaelc