SessionsController#中的NoMethodError创建未定义的方法`[]'为零:NilClass

时间:2016-05-26 14:36:10

标签: ruby-on-rails

我收到的错误是我无法弄清楚的。这是我在Github上的整个项目的链接:https://github.com/adamskriger/props_data_modeling。我将在下面粘贴相关错误和我的代码的相关部分。如果有人能弄明白为什么我会收到这个错误,我会非常感激。我尝试过其他有类似问题的人提供的解决方案,但似乎都没有。

这是我得到的错误:enter image description here

这是我的控制台显示的内容: enter image description here 这是我的会话控制器:

```

class SessionsController < ApplicationController

  def new

  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      session[:user_id] = user.id
      flash[:success] = "You have successfully logged in"
      redirect_to user_path(user)
    else
      flash.now[:danger] = "There was something wrong with your login information"
      render 'new'
    end
  end

  def destroy
    session[:user_id] = nil
    flash[:success] = "You have logged out"
    redirect_to root_path
  end


end

```

这是我的会话/ new.html.erb

```

<h1 align="center">Log in</h1>

<%= form_for(:session, :html => {class: "form-horizontal", role: "form"}, url: login_path) do |f| %>
  <div class="form-group">
    <div class="control-label col-sm-2">
      <%= f.label :email %>
    </div>
    <div class="col-sm-8">
      <%= f.email_field :email, class: "form-control", placeholder: "Enter email", autofocus: true %>
    </div>
  </div>
  <div class="form-group">
    <div class="control-label col-sm-2">
      <%= f.label :password %>
    </div>
    <div class="col-sm-8">
      <%= f.password_field :password, class: "form-control", autocomplete: "off" %>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <%= f.submit "Log in", class: 'btn btn-primary btn-lg' %>
    </div>
  </div>
<% end %>
<div class="col-xs-4 col-xs-offset-4">
  [ <%= link_to "Cancel request and return to articles listing", props_path %> ]
</div>

``` 这是我的用户控制器

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

  # GET /users/1
  # GET /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:username, :email, :password)
    end
end

enter image description here

1 个答案:

答案 0 :(得分:0)

嗨,请尝试替换下面的form_for语法

<%= form_for :session, url: login_path, html: {class: "form-inline"} do |f| %>

这可能会有所帮助: - )