New to ror and confused

Hi,

I have been learning ROR for less than a month. I am having problems
getting my head around models and controllers.

I want to have 2 tables called players and results in my DB. I want to
be able to access them in a browser to show all the records by /player
and /result. I also want to use /admin to add new players and results.

How would I go about this? I am not asking for specific code, just the
commands I need to generate the appropriate controllers and models.

Thanks

Here are some good cheat sheets and other such stuff that helped me when
I first got started and believe me it gets easier but it does take time
to wrap your mind about how everything works:

http://www.slash7.com/ Look at the “Goodies”

http://blog.invisible.ch/files/rails-reference-1.1.pdf

http://www.blainekendall.com/uploads/RubyOnRails-Cheatsheet-BlaineKendall.pdf

and this book helped me a lot when I first started out:

The cheatsheets should give you a good start and should answer your
questions. Good luck.

First let me say there are many different ways to implement this. It
depends on your particular needs.

So I will try to break down the conventional approach presented by
Ruby on Rails.

You are describing two resources: a Player and a Result. This would be
represented by two models and in turn two database tables.

The models would be named Player and Result. The database tables and
resource routes would be the plural of those (i.e. players and
results).

You would have the the following routes for manipulating those
resources:

GET: /players
GET: /players/1
POST: /players
PUT: /players/1
DELETE: /players/1

When you GET (as in HTTP GET method) on /players you are asking for a
list of players. This will be handled by the index action and players/
index.html.erb page in your controller and view respectively.

If you POST to that same /players route you would be asking the
controller to create a new Player instance and save it to the players
database table. This is handled by the create action in your
players_controller. Of course, there also needs to be a way to ask
(GET) the form used to provide the attributes for a new player. You
accomplish this by sending a GET request to the /players/new path.
This means “Get the form used to submit a new player.”

Similarly, If you PUT to the path /players/1 you are asking the
players_controller to update an existing player who’s id is 1. Like
new there is also the /players/1/edit used to get the edit form for
the player with id 1 where the user would be presented the template
represented by players/edit.html.erb.

Now this is an over simplification of how the system works, but
hopefully it will give you some direction on what to study. My first
suggestion is to find and read all you can on Model-View-Controller
design pattern and on Representation State Transfer (REST). Without a
good understanding of these architectures you will be constantly
confused by Ruby on Rails.

P.S. You mentioned having an admin interface. There is a facility in
Rails to supporting admin interfaces separately from standard
interfaces, but I’m afraid that topic is too advanced for your current
level of experience with Rails. My suggestion would be to avoid the
notion of a separate admin interface until you understand the basics
of Rails.

Now, following on from Agile Web D.s with Rails, I have created
a user model and login controller, with login/login login/add_user etc…

But I cant seem to access login/add_user but I can access login/index. I
get this error:

Unknown action

No action responded to show

How would I get this to work?

Rob Pa wrote:

Now, following on from Agile Web D.s with Rails, I have created
a user model and login controller, with login/login login/add_user etc…

But I cant seem to access login/add_user but I can access login/index. I
get this error:

Unknown action

No action responded to show

How would I get this to work?

Can u put up some code examples? It sounds like you have a redirect
somewhere that is redirecting to a page called ‘show’ but no such page
exist. What does the action for ‘add_user’ look like?

Hi,

I now got it working. I had map.resources :login which will have
created the show thing.

I have now removed all the map.resources that I had and everything now
works. But Im puzzled why it wouldnt work to start with, i.e. when I
used to access /login it never worked.

Thanks a lot

Now,

I want to add a review section, so I do;

generate/model Review title:string comment:text date:date

The database table is created fine. So I then make the controller;

generate/controller reviews index. This works fine and creates
everything.

But when I try and access it using /reviews I get;

Routing Error

No route matches “/reviews” with {:method=>:get}

Why?

Thanks for the help.

I started my project again creating the models and controllers for each
section.

What I didnt realise is that I had to put;
map.resources :player
map.resources :result

into the routes section. It it now works when I access /player and
/result but is this the only and suitable way to do it?

Thanks a lot

It looks like you’re mostly struggling with the rails routing. Make
sure you read a bit about it to fully understand the concepts. A few
remarks based on your questions:

  • When you create a resource mapping (map.resources :reviews), you are
    creating all the necessary routes to perform standard CRUD (create/
    read/update/delete) actions on that resource. So Rails will
    automatically generate a route mapping /reviews to match :controller
    => ‘reviews’ and :action => ‘index’. Also it will give you a route
    mapping ‘/reviews/id’ with method GET to map to :controller =>
    ‘reviews’, :action => ‘show’, :id => id.

  • If you don’t add any routes to the routes.rb, make sure you have the
    default routes defined (map.connect ‘:controller/:action/:id’). This
    way the URL ‘/reviews’ should resolve to :controller => ‘reviews’ and
    with no further parameters it will default to the ‘index’ action of
    that controller.

  • If you want to use the resource mapping (I suggets you do for the
    application you’re making, as it looks lilke standard CRUD) and want
    to add custom actions, you need to specify them in your routing. For
    example, with map.resources :reviews, if you want to add a method
    ‘delete_all’ that deletes all reviews at once. You may want to call
    that with ‘/reviews/delete_all’ and method PUT (never use GET to
    delete something). This method is acting on all resources (a
    collection), so the route should be:
    map.resources :reviews, :collection => { :delete_all => :put }.
    If you want to have a custom action acting on a specific resource,
    e.g. ‘/reviews/3/give_rating’, then your action is on a member and the
    route would be
    map.resources :reviews, :member => { :give_rating => :put }.

  • If you get a routing error, just look at the URL called and try to
    resolve it yourself in routes.rb by reading from top to bottom. The
    fact that ‘/reviews’ doesn’t resolve means that obviously you don’t
    even have the default fallback route ‘:controller/:action/:id’
    anymore.

Hope this helps. I find chapters 3 & 4 of ‘The Rails Way by Obie
Fernandez’ extremely useful and complete when it comes to understand
routing.

One more comment: Don’t forget to stop/start your server after
changing anything in routes.rb. It can get very confusing if you
forget to do that, as changes you make are not picked up when you’re
testing your routes.

Heroku shouldn’t make a difference, however, you may need to check if
the server is restarted properly after you change your routes, as the
procedure is different than on your local machine.

Thanks for your help. Will it make a difference if I am using Heroku to
simply develop/test the site?

I think it might me Heroku, as I have just gone back into my project and
/reviews now works.

Stupid thing!

Thanks again!

So with this as my routes.rb all should work fine?

ActionController::Routing::Routes.draw do |map|

The priority is based upon order of creation: first created ->

highest priority.

Sample of regular route:

map.connect ‘products/:id’, :controller => ‘catalog’, :action =>

‘view’

Keep in mind you can assign values other than :controller and

:action

Sample of named route:

map.purchase ‘products/:id/purchase’, :controller => ‘catalog’,

:action => ‘purchase’

This route can be invoked with purchase_url(:id => product.id)

Sample resource route (maps HTTP verbs to controller actions

automatically):

map.resources :products

Sample resource route with options:

map.resources :products, :member => { :short => :get, :toggle =>

:post }, :collection => { :sold => :get }

Sample resource route with sub-resources:

map.resources :products, :has_many => [ :comments, :sales ],

:has_one => :seller

Sample resource route within a namespace:

map.namespace :admin do |admin|

# Directs /admin/products/* to Admin::ProductsController

(app/controllers/admin/products_controller.rb)

admin.resources :products

end

You can have the root of your site routed with map.root – just

remember to delete public/index.html.
#map.root :controller => “index”

See how all your routes lay out with “rake routes”

Install the default routes as the lowest priority.

map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’
end