Reputation: 11
I am having an error and hoping somebody could help and give me some clues what to do with this. I am following Agile development book in a way selectively and while I copied their code, the result is as follow:
Routing Error
No route matches [POST] "/sessions/new"
Try running rake routes for more information on available routes.
This is what I have in my routes.rb file:
get 'admin' => 'admin#index'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
resources :users
In my session_controller: skip_before_filter :authorize def new end
def create
user = User.find_by_name(params[:login_name])
if user and user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to admin_url
else
redirect_to login_url, alert: "Invalid user/password combination"
end
end
def destroy
session[:user_id] = nil
redirect_to users_url, notice: "Logged out"
end
In my session#new.html.erb :
<% if flash[:alert] %>
<p id="notice"><%= flash[:alert] %></p>
<% end %>
<%= form_tag do %>
<fieldset>
<legend>Please Log In</legend>
<div>
<%= label_tag :login_name, 'Login name:' %>
<%= text_field_tag :login_name, params[:login_name] %>
</div>
<div>
<%= label_tag :password, 'Password:' %>
<%= password_field_tag :password, params[:password] %>
</div>
<div>
<%= submit_tag "Login" %>
</div>
</fieldset>
<% end %>
Also I am using has_secure_password and have recently updated to Ruby v 193 and Rails 3.2.8. I am only still learning ROR and therefore will appreciate your help a lot- I have been trying to figure it out myself but I was not able to.
Upvotes: 1
Views: 2677
Reputation: 157
What do you get when running 'rake routes'? Could you copy that? Also, could you copy the HTML code rendered by the browser of your form?
Upvotes: 0
Reputation: 7910
You're currently posting to the same page which the form is on.
Since your view is the new
action in the sessions
controller, it is submitting a POST
to sessions/new
. Rails is then looking for a POST
route to sessions#new
. Since none exists, it's erroring.
However if you look at your routes, you can see you have a post 'login' => :create
route which actually goes to the create
action you set up to handle the form submission. Therefore if you submit the form to /login
the route will nicely POST everything through to the intended action.
To do this, change:
<%= form_tag do %>
to
<%= form_tag('/login') do %>
Also, I strongly recommend you read this: http://guides.rubyonrails.org/routing.html which will give you a better overview of how rails routing works.
EDIT: I've just seen you're probably resubmitting to /login which then should work... I think it will still default to submitting to the current action however which is officially sessions/new so can you give my suggestion a go and see if it makes a difference?
Upvotes: 1