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.
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:
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?
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?
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.
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.