6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

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| %> ⭕️

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

6
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?