Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How: Ruby on Rails to build a basic site [closed]

I'm a compsci student who wants to get learn a little about web development -- I learn best by doing. I know basic html/css/php/javascript/xml, but since Ruby is one of my favourite scripting languages, I figured I'd learn Ruby on Rails.

I'd like to build a basic website for a friend's club at school that just provides information about their organization and services they offer, and have an admin panel on it that contains a very basic inventory system (item, number in inventory, cost -- that's it) in order to learn Ruby on Rails. I'll be hosting it on a computer on campus -- so I don't have to worry about hosting.

This might sound a little silly, but as someone who's never built a website themselves, I was wondering how exactly one goes about it with rails -- like, how do I make a basic layout for the main part of the site -- with things like "Home, About Us, Services, Contact, Club Executive" along the top? Do I have to make it in html and put it in the "view" section? The tutorials I've read on rails (Getting Started With Rails) actually make the basic inventory system seem easy, compared to this part, using a lot of the built in functionality of Rails and scaffolding. The Rails documentation is a tad bit confusing.


1 Answers

The "official" Rails book is quite good if you want to start building Rails applications. link

But actually is something like this:

  1. Create the rails app using rails applicationname
  2. Create the controllers. It seesm that for you one controller is actually enough, name it for example main: ruby script/generate controller main
  3. Now you have a controller in app/controllers, called main_controller.rb. Here you can insert the actions you want this controller to respond. If you don't want the controller to do anything just show the view, then leave the method empty.

    class Main < ActionPack::Controllers
    def index
    end
    def about
    end
    def contact
    end
    (...)
    end
    
  4. Now you got a controller that will respond to index, about and contact.

  5. Create the views for this controller in app/views/main/index.erb (and others, like about.erb)
  6. You can simply use HTML if you want
  7. Alternatively you can use a layout, that you have to define in app/views/layouts/main.rhtml In this layout use HTML, but wherewer you want to include the view, write <%= yield %> Example:

    <HTML>
    <BODY>
    <%= yield %>
    </BODY>
    </HTML>
    
  8. You can include this layout in the controller by writing layout :main in the class (before the method declarations)

  9. Now if you run ruby script/server in the root of the app you can access the pages you've created. They will be static of course, but this might get you going. You have to add models and some logic to your controllers to advance. I advice you to check the book I linked if you're interested in more, or check the alternatives of rails like merb (http://merbivore.org) which has some nice features and is usually faster, but lacks the maturity of rails.
like image 102
SztupY Avatar answered Nov 18 '25 19:11

SztupY