我是ruby on rails的新手,我想为每个已登录的用户创建一个会话。 但我得到了那个错误。 在我的控制器中,我有:
class SessionsController < ApplicationController
def new
end
def create
user =User.find_by pseudo :params[:pseudo]
if user
session[:user_id]=user.id
flash[:notice]= "you are signed in! "
redirect root_url
else
flash.now[:alert]= "wrong password/pseudo"
render 'new'
end
end
def destroy
session[:user_id]=nil
flash[:notice]='You are now signed out'
redirect root_url
end
end
在我的new.html.erb中:
<h1>Sessions#new</h1>
<%= form_tag sessions_path do %>
<div>
<%=label_tag 'pseudo', 'pseudo' %>
<%= text_field_tag 'pseudo' %>
</div>
<div>
<%= label_tag 'password', 'password' %>
<%= password_field_tag 'password' %>
</div>
<%= submit_tag 'log in' %>
<% end %>
在我的索引中我有:
<h1>Users#index</h1>
<% if current_user.present? %>
Welcome <%= current_user.pseudo %> | <%= link_to 'Sign out' , logout_path %>
<% else %>
<%= link_to 'Register', new_user_url %><br/>
<%= link_to 'SignIn', login_path %>
<% end %>
在application_controller.rb中:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
helper_method :current_user
private
def current_user
User.find_by id: session[:user_id] if session[:user_id]
end
end
并且该错误与&#34; user = User.find_by pseudo:params [:pseudo]&#34;相关。我不明白为什么