Rails 3 Routing Error - "No Route Matches"
I'm following along with the O'Reilly Rails book, but it's created for Rails 2, so I think that's where the error lies.
In app/views/entries/sign_in.html.erb:
<html>
<head><title>Hello <%=h @name %> </title> </head>
<body>
<h1> Hello <%=h @name %></h1>
<%= form_tag :action => 'sign_in' do %>
<p>Enter your name:
<%= text_field_tag 'visitor_name', @name %> </p>
<%= submit_tag 'Sign in' %>
<% end %>
</body>
</html>
And in app/controllers/entries_controller.rb:
class EntriesController < ApplicationController
def sign_in
@name = params[:visitor_name]
end
end
When I click the 'Sign In' button, it takes me to a page that says:
Routing Error
开发者_StackOverflow中文版No route matches "/entries/sign_in"
I would post my routes.rb file, but it seems that it's all commented out except for this line:
get "entries/sign_in"
This file seems different than the one they're referencing in the book, so that's why I believe this to be the problem.
Thanks for your help!
If you're learning Rails, you'd find it a lot easier to be using the same version of Rails as the book you're learning it from. Either learn in Rails 2 and upgrade to 3 later, or get a new book.
There are a lot of differences between Rails 2 and 3, so you're going to be spending all your time getting confused with things like this.
Rails 3 router has changed a lot. See this blog post: http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/
What you want to do might be:
resources :entries do
collection do
post :sign_in
end
end
This adds an action on the whole collection (/entries/sign_in) of entries (in contrast to #member, which would be accessed via /entries/:id/sign_in). Though I'm not sure why you'd want to sign in someone in the entries resource. You might create a seperate controller for that.
精彩评论