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 SO 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.
将您的路由从login
更改为login_attempt
,如果您已经定义了login
操作,则添加另一个名称为login_attempt
路由
It looks like the answer is that :action isn't the correct syntax anymore. Had to change it to :method
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.