当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails - NoMethodError in SessionsController#create undefined method `[]' for nil:NilClass

问题描述:

I am getting an error that I have not been able to figure out. Here is a link to my entire project on Github: https://github.com/adamskriger/props_data_modeling. I will paste the relevant error and the relevant parts of my code below. If anyone can figure out why I am getting this error, I would very much appreciate it. I have tried the solutions offered by others with similar problems and none seem to work.

This is the error I am getting:

This is what my console shows:

This is my sessions controller:

```

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

```

This is my sessions/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>

```

This is my users controller

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

网友答案:

hi please try replacing your form_for syntax like below

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

It may help :-)

分享给朋友:
您可能感兴趣的文章:
随机阅读: