Help us understand the problem. What is going on with this article?

Ruby on Rails やってる時にNoMethodError in SessionsController に悩んだ件

More than 1 year has passed since last update.

Ruby on Rails チュートリアル(8章)を進めていく中でこんなエラーが・・

スクリーンショット 2018-03-02 12.01.57.png

NoMethodError in SessionsController

undefined method `[]' for nil:NilClass

なんだろう・・・??

値が入っていないのかな・・と思って該当の部分を見て見る・・。

sessions_controller.rb
class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      log_in user
      redirect_to user
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
    log_out
    redirect_to root_url
  end
end

よくわからん。
あっとるやん・・・ってViewを見て見ると・・やらかしていた。

new.html.erb
<% provide(:title, "Log in") %>
<h1>Log in</h1>

<div class="row">
  <div class='col-md-6 col-md-offset-3'>
    <%= form_for(:sessions, url: login_path) do |f| %>

    <%= f.label :email %>
    <%= f.email_field :email, class:'form_control' %>

    <%= f.label :password %>
    <%= f.password_field :password, class:'form_control'%>

    <%= f.submit "Log in", class:"btn btn-primary" %>
  <% end %>

  <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>

このコードの部分
<%= form_for(:sessions, url: login_path) do |f| %> ❌
<%= form_for(:session, url: login_path) do |f| %> ⭕️

見落として結構時間取られてしまってた・・。
気をつけないと・・泣

tibiprogrammer
ごく普通の女子大生
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away