处理 Rails 中的参数

Handling Parameters in Rails

如何验证从视图传递给控制器​​的参数。以下是我的查看文件。

index.html.erb:

<html>
<body>
    <center> <h1> Login form </h1> </center>
    <form action='/login/auth' method='GET'>
        Name: <input type="text" name="username" width=20>
        Password: <input type="password" name="password" width=20>
        <input type="submit" value="login">
    </form>
</body>
</html>

以上是客户端调用'/login'控制器时在客户端渲染的视图文件。因此,一旦值被填充并且 按提交按钮,数据将传递给“/login/auth”操作。在 auth 方法中如何处理从中传递的数据 客户端。我的控制器文件是,

login_controller.rb:

ass LoginController < ApplicationController
  def auth

  end
end

像这样:

if params[:username].present? && params[:password].present? 
  #do something
end

您可以从 params[:用户名]参数[:密码].

为了简单安全的身份验证过程,您可以使用广泛用于身份验证的 "devise" gem .它易于配置并默认提供登录和注册视图页面。

URL 关注 https://github.com/plataformatec/devise