Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用参数中的键_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 使用参数中的键

Ruby on rails 使用参数中的键,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正试图通过本教程构建一个登录系统: 我的表单如下所示: <!DOCTYPE html> <div id="content"> <%= flash[:alert1] %> <%= form_for(:sessions, :url => sessions_path , :html => {:id => "login-form"}) do |f| %> <fieldset> <p>

我正试图通过本教程构建一个登录系统:

我的表单如下所示:

<!DOCTYPE html>
<div id="content">
  <%= flash[:alert1] %>
  <%= form_for(:sessions, :url => sessions_path , :html => {:id => "login-form"}) do |f| %>
    <fieldset>
      <p>
        <%= label_tag :name ,"Username:" %>
        <%= text_field_tag :name, params[:name] , :class => "round full-width-input", :autofocus=>true %>
      </p>
      <p>
        <%= label_tag :password, "Password:" %>
        <%= password_field_tag :password, params[:password], :class => "round full-width-input" %>
      </p>
      <%= submit_tag "Login",  :class=> "button round blue image-right ic-right-arrow" %>
    </fieldset>
    <% if (flash[:status] == FALSE) %>
      <br/><div class="information-box round"><%= flash[:alert] %></div>
    <% end %>
  <% end %>
</div> <!-- end content -->
class SessionsController < ApplicationController
  def login
  end

  def create
    user = User.authenticated?(params[:sessions][:name], params[:sessions][:password])

    flash[:alert1] = "dummy"
    if user
      redirect_to '/login'
    else
      flash[:status] = FALSE
      flash[:alert] = "Invalid username and password"

      redirect_to '/login'
    end
  end

  def new
  end

end
我的控制器如下所示:

<!DOCTYPE html>
<div id="content">
  <%= flash[:alert1] %>
  <%= form_for(:sessions, :url => sessions_path , :html => {:id => "login-form"}) do |f| %>
    <fieldset>
      <p>
        <%= label_tag :name ,"Username:" %>
        <%= text_field_tag :name, params[:name] , :class => "round full-width-input", :autofocus=>true %>
      </p>
      <p>
        <%= label_tag :password, "Password:" %>
        <%= password_field_tag :password, params[:password], :class => "round full-width-input" %>
      </p>
      <%= submit_tag "Login",  :class=> "button round blue image-right ic-right-arrow" %>
    </fieldset>
    <% if (flash[:status] == FALSE) %>
      <br/><div class="information-box round"><%= flash[:alert] %></div>
    <% end %>
  <% end %>
</div> <!-- end content -->
class SessionsController < ApplicationController
  def login
  end

  def create
    user = User.authenticated?(params[:sessions][:name], params[:sessions][:password])

    flash[:alert1] = "dummy"
    if user
      redirect_to '/login'
    else
      flash[:status] = FALSE
      flash[:alert] = "Invalid username and password"

      redirect_to '/login'
    end
  end

  def new
  end

end
尝试提交时,我遇到以下错误: nil:NilClass的未定义方法“[]” 在以下行中: user=user.authenticated?参数[:会话][:名称],参数[:会话][:密码]

我是否在会话密钥中意外使用了

谢谢,
姑娘

看起来您正在使用外部身份验证gem,可能是其中之一?


您需要在顶部包含一个require行。

我认为您的表单中存在一些问题:您使用表单,然后在字段中使用文本字段标记。 我会用如下方式纠正它:

<% form_for sessions .... do |f| %>
<%= f.text_field :name %>
我建议您使用一些gem,而不是构建一个完整的身份验证系统,这在安全性方面可能相当棘手。你看了吗? 希望能有帮助