Simply Rails2 - Chapter 8

I’m stuck again. Anyone know where I should look to fix this:

session_url failed to generate from {:controller=>“session”, :action=>“show”} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: [“session”, :id] - are they all satisfied?

I stumbled on the same problem, myself. I saw that ‘session’ was singular, but completely missed that they were also stating that ‘map.resource’ should be singular as well.

Thanks to fool4jesus for asking the question, and atetlaw for helping us get to the answer. :slight_smile:

Does your config\routes.rb file look like

ActionController::Routing::Routes.draw do |map|
  # The priority is based upon order of creation: first created -> highest priority.

......

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   map.resources :products
  map.resource :session
  
......

  # Sample resource route with sub-resources:
  #   map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
  map.resources :stories, :has_many => :votes
  
......

  # Install the default routes as the lowest priority.
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

Thanks Mittineague,
I had the order (priority) wrong, but now I’m getting the following error:

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

Why is it looking for “sessions” - I thought it should be singular “session”?

Sorry Mittineague,
I found the ‘plural’ problem - I had entered “sessions/new” in the address of the browser.

However when I enter “session/new” I get the following error:

uninitialized constant SessionsController

Any helpful information in the shovell\log\development.log file?

Any idea what this means?

Rendering c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/templates/rescues/layout.erb (internal_server_error)

My apologies for the delay in posting back.
I have tried doing a few things to replicate the error, but without luck.
The file is in the actionpack gem
layout.erb

<html>
<head>
  <title>Action Controller: Exception caught</title>
  <style>
    body { background-color: #fff; color: #333; }

    body, p, ol, ul, td {
      font-family: verdana, arial, helvetica, sans-serif;
      font-size:   13px;
      line-height: 18px;
    }

    pre {
      background-color: #eee;
      padding: 10px;
      font-size: 11px;
    }

    a { color: #000; }
    a:visited { color: #666; }
    a:hover { color: #fff; background-color:#000; }
  </style>
</head>
<body>

<&#37;= @contents %>

</body>
</html>

but I don’t see how it could give a rendering error. The only thing I can think of that might be causing an internal server error is that “@contents” is failing somehow somewhere.

I am generally quite happy with this book, but I think they’ve fallen down a bit on this session/sessions thing. Why do you use sessions in one place and session in another? Which do you use where, and how would one ever remember which to use? I’m afraid that the explanation on page 261 is more confusing than helpful.

And, of course, I am getting the exact same result as taluswood.

I would very much appreciate somebody in the know explaining this. Has anybody gotten the chapter 8 example to work?

This might help clarify the issue. On page 260 there’s an explanation for the use of: map.resource :session

Please note that we’ve used the singular form of map.resource instead of the plural form (map.resources) that we’ve been using for stories. When using the singular form, Rails knows we’re talking about a singleton resource, which means that only one of it ever exists at a time. This is true here in the context of a User object, which is only ever going to have a single session at a time. As such, all the RESTful URLs for sessions will take the singular form, rather than the plural form, of the model name we’ve seen so far. For example, the URL that creates a new session will be: /session/new.

Thank you, sir, you are exactly right. I misread the sentence in the book to say that the resource name “stories/story” was singular, not the ruby function name. Upon re-reading the passage in question, I see that it does indeed say that. On the other hand, that’s apparently something easy to miss (since others have as well), and I did until I compared the singular and plural routes side-by-side. For a future edition of the book, you might consider showing a plural example next to the singular one instead of just an ellipsis. I still do think a more explanatory section about routes.rb would be helpful.

Thanks for your help!