如何将`form_tag`转换为`simple_form_for`进行登录?

问题描述 投票:0回答:1

这个片段工作正常:

<%= form_tag(session_path) do |f| %>
   <p>
       <%= label_tag :email %>
       <%= email_field_tag :email, nil %>
    </p>
    <p>
       <%= label_tag :password %>
       <%= password_field_tag :password, nil %>
    </p>
        <%= submit_tag "Sign in", class: 'btn-primary' %>
<% end %>

但是当我试图按照屏幕截图将其重构为simple_form_for时,它给了我错误无效的电子邮件或密码! enter image description here

# sessions_controller.rb
class SessionsController < ApplicationController
    def new
    end

    def create
        if user = User.authenticate(params[:email],params[:password])
            session[:user_id] = user.id
            flash[:notice] = "Signed in successfully."
            redirect_to user
        else
            flash.now[:alert] = 'Invalid email or password!'
            render :new
        end
    end
end

我觉得这行有问题

<%= simple_form_for :session, url: session_path do |f| %>

routes.rbresource :session - 是的,它是单数,而不是复数

ruby-on-rails forms ruby-on-rails-5 simple-form
1个回答
1
投票

当您添加simple_form_for :session时,表单开始发送包含在session键中的所有参数:session: { email: '[email protected]', password: 'password' }。如果检查服务器日志,你会看到它

更改控制器:

User.authenticate(params[:session][:email], params[:session][:password])
© www.soinside.com 2019 - 2024. All rights reserved.