SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Combining Rails controllers in one view

    I'd like to let people add comments to an article and I'd like the comment entry field and save button as well as previous comments to appear on the same page as the article itself.

    I've got an Article model and a Comment model. Article has_many Comments and Comment belongs_to Article. Article is RESTful; not sure about Comment yet.

    In my ArticleController "show" action, I believe I'll need to put some of the same code as I would put in the Comment "new" action. That way, I've got a new comment instantiated in case a user decides to add a comment.

    Is there a best practice for this sort of thing -- or a tutorial? Any suggestions?

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends how much coding is shared and how widely you want to make it available. There are two DRY options:
    1. Put the shared code into an application method (in application.rb) and then accesses it by calling the method from within each of the controller methods that need to run that code
    2. Create a module that contains methods that incorporate the required code and then mixin the code in the controllers where it is required.

    If the code is relatively small and needed across many of your controllers, then option 1 is usually best. If the code is relatively large and used by only a small number of controller, then option 2 is probably better. Between these two extremes, it's a case of personal choice as to which to use.

    consider these two methods:

    a. A method added to application.rb
    Code:
    def add_a_to_b(a, b)
      a + b
    end
    b. A module created as /lib/subtraction_stuff.rb
    Code:
    module SubtractionStuff
    
      def subtract_a_from_b(a, b)
        a - b
      end
    end
    To use the method add_a_to_b within a controller, you'd simply add:
    Code:
    @result = add_a_to_b(1,5)
    To use the method subtract_a_from_b you need to do this:
    Code:
    class SomeController < ApplicationController
    
      include SubtractionStuff
    
      def show
        @result = subtract_a_from_b(40, 10)
      end
    
    end
    That is to mixin the module using an include statement, and then use the method within a controller method.

  3. #3
    l 0 l silver trophybronze trophy lo0ol's Avatar
    Join Date
    Aug 2002
    Location
    Palo Alto
    Posts
    5,329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's actually a really simple way to describe modules in general, Reggie... thanks, it helps clarify an area a bit that I haven't dove into too much yet.

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad you like it. I think worked examples are often the easiest way to explain things.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •