change your route from login
to login_attempt
, if you already have login
action defined then add another route with name login_attempt
UrlGenerationError for login page Rails 4.1
-
14-07-2023 - |
题
I am trying to create a login page. I am following this guide. However I have gotten stuck with the following error when trying to access my sessions/login page. I am using Rails 4.1. I looked at other answers for questions similar to this on S.O. but I was unable to find anything that seemed to relate. This was my error:
ActionController::UrlGenerationError in Sessions#login
No route matches {:action=>"login_attempt", :controller=>"sessions"}
And the line that is highlighted is:
<%= form_tag(:action => 'login_attempt') do %>
This is my login page template:
<% @page_title = "Foos-Tracker | Login" -%>
<div class= "Sign_Form">
<h1>Log in</h1>
<%= form_tag(:action => 'login_attempt') do %>
<p>Username or Email:</br> <%= text_field_tag(:username_or_email) %></p>
<p>Password:</br> <%= password_field_tag :login_password %></p>
<%= submit_tag("Log In") %>
<% end %>
</div>
My Controller:
class SessionsController < ApplicationController
before_filter :authenticate_user, :only => [:home, :profile, :setting]
before_filter :save_login_state, :only => [:login, :login_attempt]
def login
#Login Form
end
def login_attempt
authorized_user = User.authenticate(params[:username_or_email],params[:login_password])
if authorized_user
session[:user_id] = authorized_user.id
flash[:notice] = "Wow Welcome again, you logged in as #{authorized_user.username}"
redirect_to(:action => 'home')
else
flash[:notice] = "Invalid Username or Password"
flash[:color]= "invalid"
render "login"
end
end
end
And my routes:
Rails.application.routes.draw do
get 'sessions/login'
get 'sessions/home'
get 'sessions/profile'
get 'sessions/setting'
get 'welcome/index'
get 'users/new'
resources :users
root 'welcome#index'
end
Any ideas / help would be greatly appreciated. Thanks.
解决方案
其他提示
It looks like the answer is that :action isn't the correct syntax anymore. Had to change it to :method
不隶属于 StackOverflow