Wai, thanks for the suggestions (and your patience !).
I don’t think filters are what I’m after and the validation is already
within my model class (Comment)…
I guess this is coming down to a stylistic thing (and, as I said,
perhaps a hangup from Java MVC) but in my understanding, controllers
are the exposed facade for CRUD style actions. All I am really asking
is why one controller can’t use another in the same request scope ?
- NewsController posts to CommentsController to add a comment.
BTW, is there any particular reason why the NewsController would post to
another Controller instead of a Model?
Badly worded on my part, sorry. What I mean is the HTML form, rendered
by a NewsController.show, POSTs to CommentsController.create (actually
just via “/comments” with a POST and the ActiveResource stuff takes
care of the mapping to CommentsController.create).
The comments form is wrapped up in a partial that I can reuse in any
view (say, Interview). Which would also POST to
CommentsController.create…
The comments model (acts_as_commentable) has the context and the id of
it’s creator ([NEWS,1,“a comment”,“matt”],[NEWS,2,“another comment”,
“fred”], [INTERVIEW,1,“great interview”,“john”]). So thats all well
and good. And, upon a successful creation, CommentsController
redirects back to it’s caller (News or Interviews). It’s just when not
successful, I’d like to FORWARD back to the caller without new request
scope or session munging.
Perhaps the code may help (CommentsController.create):
def create
@comment = Comment.new(params[:comment])
@comment.commentable_type = params[:commentable_type];
@comment.commentable_id = params[:commentable_id];
object_to_comment = eval "#{@comment.commentable_type}.find
#{@comment.commentable_id}"
respond_to do |format|
if @comment.save
object_to_comment.add_comment @comment
flash[:notice] = 'Comment was successfully created.'
# hmmmmm ! stinky ??
format.html { redirect_to_url "/
#{@comment.commentable_type}/#{@comment.commentable_id}" }
format.xml { head :created, :location =>
comment_url(@comment) }
else
flash[:notice] = “You didn’t enter a comment ?”
format.html do
# I don’t like this but I still don’t get how to
ActionForward ?
session[:comment] = @comment
redirect_to :controller =>
object_to_comment.class.to_s, :action => “show”, :id =>
object_to_comment.id, :anchor => “commentsForm”
end
format.xml { render :xml => @comment.errors.to_xml }
end
end
end
Matt