SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Agile Web Development with Rails - Questions Here

    Agile Web Development with Rails seems to be the bible for newbs to ruby and rails. i'm workign with the 1st edition - so be sure to mention if you are working with the 2nd edition, otherwise, folks may get confused.

    while reading this book, there is a definite process that is followed to do this "right." i can see where someone unfamiliar with the process could butcher RoR to the point much of its benefits are lost. i don't want to be that guy.

    i went through the hello world tutorial pretty smoothly.

    the depot application has caused a few snags, though. first, they authors start designing the application without delving so much into the whys of the program design. i know they penciled up some templates, but they didn't discuss how that impacts the RoR program design. i've been able to figure out some aspects of it, but i'm not confident i have it all down.

    for example, why two controllers? i think the use cases where there was a buyer and a seller is supposed to yield the clue. but what about apps that aren't so clear? okay, this may be an mvc question, but RoR is mvc, too.

    what is the generic thought process that yields the number of controllers for any given app? maybe they touch on this later in the book. maybe not.

    there also appear to be different types of controllers.

    1. admin controller - it has a scaffold and is linked to a db table. i'm a bit curious how one can create a scaffold based on related tables... but maybe this is too soon. i do hope it is addressed, though.

    2. the store controller - this seems to be a standard controller with a default index "action method." what is the definition of an action method? i might've missed that in the ruby tutorial portion of the book.

    i also got a little confused when they started creating methods all over the place and didn't really delve into why they did what they did where they did. i think this will work itself out as i reread the text.

    wrt to the ruby tutorial:

    on page 486 (ruby tutorial), they through out:

    Code:
    def say (phrase)
      puts "#{phrase}"
    end
    i missed where they discussed what the # does. can anyone help? now that i look at it, what do the brackets do (why brackets, not parenthesis)?

    on page 486, they unleash "initialize" w/o discussing it details. i think it is related to new method, but i'd like a more a thorough discussion.

    what does ":" do in ":cart"? i know i likely missed this in the ruby tutorial. i thin it has something to do with ids and/or passing variables.

    okay, the million dollar question - how to get rid of typing the obnoxious port 3000 in the urls?

    anyway, i'd appreciate a more thorough discussion of these issues if anyone cares to share their knowledge. also, any other Agile Web Dev newbies should chime in with questions here, too. please make them book related so future newbs can use this as a resource to get their questions answered.

  2. #2
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what does "full stack" framework mean?

    what is an example of a non full stack framework and why isn't it "full stack?"

    tia...

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by skeeterbug
    what does ":" do in ":cart"? i know i likely missed this in the ruby tutorial. i thin it has something to do with ids and/or passing variables.
    i found this myself... :cart is a symbol.

    Thomas defined a symbol as:

    "Symbols are like constants whose name is their value. Just like the number 5 has both the name 5 and the value 5 so the symbol :howdy is the word howdy. It’s just like “howdy” but Better™

    that’s pretty neat."

    this was taken from this blog and response page...

    http://glu.ttono.us/articles/2005/08...g-ruby-symbols

  4. #4
    SitePoint Enthusiast Stevenwulf's Avatar
    Join Date
    May 2002
    Location
    Berkeley
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The initialize method:
    I'm not sure if you're familiar with other object oriented languages, but basically it is the object's constructor. Everytime you call something like Dog.new, the Dog's initialize method will get called. Let's say that when creatiung a Dog, you want to make sure that it has certain attributes, this is where initialize comes in. You can always set attributes later by using setter methods, but initialize is your way off making sure that the object is born with a basic set of attributes.

    Code:
        class Dog
          def initialize(eye_color, name)
        	@eye_color = eye_color
        	@name = name
          end
        
          def eye_color()
        	@eye_color
          end
          
          def name()
        	@name
          end
        end
        
        d = Dog.new("brown",m "Fido")
        puts d.eye_color	#=> "brown"
        puts d.name    #=> "Fido"

    #{var}

    "#{some_variable}" is used for variable interpolation. If you have a string and you want to substitute variables into the string, this is how it is done. Let's say you want to have a xmas card template that gets sent to all of your friends, but you want to customize the name portions of the card:

    Code:
       friends = ["John", "Sally", "Pete"]
       
       for f in friends
         puts "Merry Xmas #{f}"
       end
       
       # out put
       "Merry Xmas John"
       "Merry Xmas Sally"
       "Merry Xmas Pete"

    Defining Controllers:
    I'm not sure if there is a hard fast rule for this one, maybe other rails devs can chime in. I have to admit, this one kinda confused me at first too. I usually have a controller for each part of my site that serves a specific purpose. Here are some examples,

    Section that lets me add,edit,list,delete products: product_controller
    Section that lets a user change settings on their account: account_controller
    Section that shows the admin site statistics: site_statistics_controller

    If one controller starts accumulating methods, maybe up to 7-10 methods, then I start thinking about splitting the functionality into 2 controllers. Again, this isn't hard fast rule, the point is that I think controllers should be light, not bloated with tons of methods.


    Port 3000:
    This is the default port used by webrick. If you want to change it to the standard prot 80, start webrick like this:

    Code:
       ruby script/server --port=80

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Skeeterbug
    1. admin controller - it has a scaffold and is linked to a db table. i'm a bit curious how one can create a scaffold based on related tables... but maybe this is too soon. i do hope it is addressed, though.
    What, like if your products had a category_id related to a Categories table? In that case you could do a scaffold for adding/editing/removing categories, but adding say a dropdown in the products scaffold to link categories to products would be your job. It is actually pretty easy though.

    Anyway, try not to rely too much on scaffolding. It's a neat feature, but the more you use Rails the less you'll actually use it.

    Quote Originally Posted by Skeeterbug
    2. the store controller - this seems to be a standard controller with a default index "action method." what is the definition of an action method? i might've missed that in the ruby tutorial portion of the book.
    This is actually part of ActionController. In an ActionController, every method basically maps to a URL. So in this controller:
    Code:
    class Product < ActionController::Base
      def index
    
      end
    
      def search
    
      end
    end
    We have two methods, "index" and "search". These are now URLs that magically map to /products/index/ and /products/search/. Basically, create a new method in a controller when you need a new page/URL to make something happen.
    Quote Originally Posted by Skeeterbug
    okay, the million dollar question - how to get rid of typing the obnoxious port 3000 in the urls?
    Code:
    ruby script/server -p 9981
    replace "9981" with whatever port number you want, assuming the port is available.

  6. #6
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, the -p option for ports only works when you are using webrick - if you have lighttpd installed on your machine, Rails will use that by default and the -p option will not do anything. What you instead have to do is, after running script/server for the first time, Rails will create a lighttpd.conf file in your config folder - open this and edit the port in there.

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    moving through the cart code (with an eye for explanations for newbs and also a few questions):

    Code:
    private
      # Return a cart object. If we already have one cached
      # in the session, use it, otherwise create a new one 
      # and add it to the session
      def find_cart
        session[:cart] ||= Cart.new
      end
    well, i can't explain it better than the notes - so there you go. this session has access to the

    1. @items - instance items array (is it correct to say instance array?)
    2. @total_price - instance variable that equals the total price

    class Cart threw me for a loop, though.

    Code:
    class Cart
      attr_reader :items
      attr_reader :total_price
      
      def initialize
        @items = []
        @total_price = 0.0
      end
    
      def add_product(product)
        @items << LineItem.for_product(product)   
        @total_price += product.price
      end
    end
    i don't understand:

    attr_reader :items
    attr_reader :total_price

    what do they do? i couldn't find an explanation the book. initialize defines @items and @total_price whenever class Cart is called. hmmmm... now that i think about it, if i had to guess

    attr_reader :items
    attr_reader :total_price

    likely read in a value if the cart already exists so that @items and @total_price aren't initialized empty when the cart already exists and has different values.

    however, that's just a guess.

    the "<<" syntax appends data to the array.

    Code:
      def add_to_cart
        product = Product.find(params[:id])
        @cart = find_cart
        @cart.add_product(product)
        redirect_to(:action => 'display_cart')
      end
    product = Product.find(params[:id])

    i think find is a method that acts on the database (a method of ActiveRecord::Base???). params[:id] is the parameter that you send at the end of the url. i need to go back and review exactly how RoR is told to send the id via the end of the url.

    when you feed params[:id], the product that corresponds to the sent parameter is found.

    it then finds the cart, if it exists, or creates a new one, if it doesn't exist. it appears the cart session is stored in a session variable. i think this is important so that the view has access to it.

    then the product is added to the items array and the total price is adjusted.

    Code:
    class LineItem < ActiveRecord::Base
    
      belongs_to :product
    
      def self.for_product(product)
        item = self.new
        item.quantity = 1
        item.product = product
        item.unit_price = product.price
        item
      end
    
    end
    table line_item is linked to product. i think all the self notation has to do with making the for_product method available through the application in the form LineItem.for_product(product).

    everything else is pretty intuitive except for "item" at the end. i don't know why it has to be there.

    Code:
      def display_cart
        @cart = find_cart
        @items = @cart.items
      end
    an instance variable of the session cart is made available.@items is assigned to the value of cart.@items (which has incorrect syntax). i assume the syntax is adjusted to @cart.items for readability purposes. or i could be wrong.

    @items.size returns the number of elements in the array and size appears to be a standard method to do this. again, this is an educated (i hope, anyway!) guess - i could be wrong.

    i'd appreciate input to answer the questions and add more in depth knowledge. not only will it help yours truly, it will help future newbs going through the agile book.

    tia...

  8. #8
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by skeeterbug
    what does "full stack" framework mean?

    what is an example of a non full stack framework and why isn't it "full stack?"

    tia...
    I'd still like a definition on this.

    I have guesses, but they are, well, guesses.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  9. #9
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by samsm
    I'd still like a definition on this.

    I have guesses, but they are, well, guesses.
    Full-stack framework. Programmers spend an enormous amount of time making a thousand different pieces work together. They cherry-pick a little over here, a little over there. Often times, the pieces are not exactly a good fit. So you need to add a bunch of glue, write a bunch of configuration, and basically end up wishing the left hand knew what the right hand was doing.

    Rails is an ambitious attempt to provide a “full-stack framework,” which means that we attack all of the problems across the model-view-control pattern. We deal with creating the templates that generate the HTML code, the controllers that direct incoming requests, and the model that makes the database come alive in objects.

    In a sense, we’re trying to be the Apple of Web application development. Provide and care for the complete experience. The buck stops here. Every problem is our problem. So we tend to get a lot of those small things right that fall outside of any of the major divisions.

    We get all those simple, unglamorous problems solved—the ones that won’t make a good article in an academic computer science journal, but that most programmers are struggling with every day.
    It's from eWeek so take it with a grain of salt

  10. #10
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That totally makes sense.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  11. #11
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm working through the agile book, and i'm trying to implement the add_user/login functionality. I've entered the code as it's written in the book, and i end up with this error:

    /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:1858:in `method_missing'
    app/models/user.rb:36:in `create_new_salt'
    app/models/user.rb:27:in `password='
    app/controllers/login_controller.rb:5:in `new'
    app/controllers/login_controller.rb:5:in `add_user'


    i know this thread is a little old, but i'm hoping someone still their eye on this one.

    thanks for the help


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
  •