I’m converting a few of my existing websites from ASP.NET to Ruby on
Rails, but I’m having a problem getting my head a round code re-use in
ROR.
For example, the site I am currently attempting to convert has
different pages, whih each contain a comment block.
A comment block includes a table of comments and a form to add another
comment.
So, I have a UsersController which handles all the pages related to a
user i.e. the users homepage, there guestbook page etc
I also have an ArtistsController which handles all the pages related
to an artist i.e. a details page, the artists “art page listings”,
their shows etc etc
I want to use the comments block (which includes the table and form)
on pages which could be handled by either controller.
I understand that I can split the comments view into a seperate
partial view, and so the view can be re-used.
BUT I dont want to have to put the code that handles interpreting the
comments (which the user has typed into the comments form) in to both
controllers!!!
How can avoid this, I think I’m missing something!!
I presume you have a comments controller. In this controller, you can
either use a helper module (module CommentsHelper in
/helpers/comments_helper), or a static method:
def self.comments_handler #your code…
end
Hope this helps…
I suggest to define a method in your ApplicationController to process
your comment, and the define before_filter in both of your
UserController and ArtistController.
I’m converting a few of my existing websites from ASP.NET to Ruby on
Rails, but I’m having a problem getting my head a round code re-use in
ROR.
If my suggestion below doesn’t help, try forum.softiesonrails.com,
there tend to be a bunch of ex-.NET devs there willing to help.
BUT I dont want to have to put the code that handles interpreting the
comments (which the user has typed into the comments form) in to both
controllers!!!
How can avoid this, I think I’m missing something!!
Your partial helps with rendering the form from different controllers,
but I’d suggest you still post your form data to the
CommentsController’s create action. Then, you can redirect from there
to wherever you want.
This helps centralize where new comments are created - in the
CommentsController - but you can still render comments from any
controller’s view by sharing the partial around.