我是
Ruby on Rails应用程序的新手,我已经创建了CRUD但仍然堆叠在Login&注销功能.
这是我的Sessions_controller:
def new end def create user = User.find_by(email: params[:session][:email].downcase) if user && user.authenticate(params[:session][:passkey]) # Log the user in and redirect to the user's show page. else # Create an error message. flash[:danger] = 'Invalid email/password combination' # Not quite right! render 'new' end end def destroy end
这是我的观点:new.html.erb
<%= form_for(:session,url: login_path) do |f| %> <%= f.label :email %> <%= f.email_field :email,class: 'form-control' %> <%= f.label :passkey%> <%= f.password_field :passkey,class: 'form-control' %> <%= f.submit "Log in",class: "btn btn-primary" %> <% end %>
这是我的模特:
class User< ActiveRecord::Base validates :first_name,:presence => true,:length => { :in => 3..20 } validates :last_name,:length => { :in => 3..20 } validates :email,:uniqueness => true,format: { with: /\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z/ } validates :passkey,:confirmation => true,:length => {:in => 6..20} end
NoMethodError in SessionsController#create
undefined method `authenticate’ for#User:0x0000000d5c65e0>
我正在使用ruby 1.9.7 Rails 4.2.5& MysqL2
那对我很有帮助,拜托.