# Code reuse with rails (ex ASP.NET developer having trouble)

**URL:** <https://www.ruby-forum.com/t/code-reuse-with-rails-ex-asp-net-developer-having-trouble/113567>\
**Category:** Rails\
**Created:** [September 1, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/code-reuse-with-rails-ex-asp-net-developer-having-trouble/113567 "2007-09-01T23:19:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [September 1, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/code-reuse-with-rails-ex-asp-net-developer-having-trouble/113567/1 "2007-09-01T23:19:54Z")

</div>

Hi,

I’m converting a few of my existing websites from [ASP.NET](http://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!!

Any help would be appreciated!

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [September 2, 2007, 12:49am UTC](https://www.ruby-forum.com/t/code-reuse-with-rails-ex-asp-net-developer-having-trouble/113567/2 "2007-09-02T00:49:32Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [September 2, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/code-reuse-with-rails-ex-asp-net-developer-having-trouble/113567/3 "2007-09-02T23:07:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [September 2, 2007, 2:09am UTC](https://www.ruby-forum.com/t/code-reuse-with-rails-ex-asp-net-developer-having-trouble/113567/4 "2007-09-02T02:09:05Z")

</div>

On Sep 1, 4:18 pm, Rick Walsh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I’m converting a few of my existing websites from [ASP.NET](http://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](http://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.

> Any help would be appreciated!

Does this help?

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

[essentialrails.com](http://essentialrails.com)
