0
2

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.

エラー文First argument in form cannot contain nil or be emptyの対処法

Posted at

前提条件は以下の通り。
・投稿にコメント機能をつける。

image.png

問題のコードは以下の通り。

gym_controller.rb
class GymsController < ApplicationController
    before_action :logged_in_user, only: [:show, :create, :destroy]
    before_action :correct_user,   only: :destroy

  def index

    if logged_in?
        @gym  = current_user.gyms.build
        @feed_items = current_user.feed.paginate(page: params[:page])
      end

  end

  def show
  @gym = Gym.find(params[:id])

  end

  #ログインしたユーザーがジムを投稿する
  def create
   @gym = current_user.gyms.build(gym_params)
   if @gym.save
       flash[:success] = "投稿ありがとうございます!"
       redirect_to request.referrer ||  'gyms_path'
       else
               @feed_items = []
       render 'gyms/index'
       end
  end

  def destroy
    @gym.destroy
  flash[:success] = "ジムを削除しました"
  redirect_to request.referrer ||  'gyms_path'
  end

  private

     def gym_params
       params.require(:gym).permit(:name, :content)
     end

     def correct_user
       @gym = current_user.gyms.find_by(id: params[:id])
       redirect_to gyms_path if @gym.nil?
end
end
views/gyms/show.html.erb
<div><%= @gym.name %></div>
<div><%= @gym.content %></div>

<div><%= render partial:'comments/comment_form' %></div>
<p></p>
<div class="col-md-6">
  <% if @comments.any? %>
  <ol class="microposts">
    <%= render @comments %>
  </ol>
  <%= will_paginate @comments %>
  <% end %>
</div>
_comment_form.html.erb
    <%= form_for(@comment) do |f| %>
        <%= hidden_field_tag :gym_id, @gym.id %>
        <%= f.text_field :content, placeholder: "Comment..." %>

        <%= f.submit "コメントする", class: "btn btn-primary btn-sm" %>
    <% end %>

こちらの記事の通りに、gym_controller.rb内のshowアクションで@comment = Comment.new記述したところ動きました。
https://teratail.com/questions/16823

gym_controller.rb
class GymsController < ApplicationController
    before_action :logged_in_user, only: [:show, :create, :destroy]
    before_action :correct_user,   only: :destroy

  def index

    if logged_in?
        @gym  = current_user.gyms.build
        @feed_items = current_user.feed.paginate(page: params[:page])
      end

  end

  def show
  @gym = Gym.find(params[:id])
  @comment = Comment.new
  end

  #ログインしたユーザーがジムを投稿する
  def create
   @gym = current_user.gyms.build(gym_params)
   if @gym.save
       flash[:success] = "投稿ありがとうございます!"
       redirect_to request.referrer ||  'gyms_path'
       else
               @feed_items = []
       render 'gyms/index'
       end
  end

  def destroy
    @gym.destroy
  flash[:success] = "ジムを削除しました"
  redirect_to request.referrer ||  'gyms_path'
  end

  private

     def gym_params
       params.require(:gym).permit(:name, :content)
     end

     def correct_user
       @gym = current_user.gyms.find_by(id: params[:id])
       redirect_to gyms_path if @gym.nil?
end
end
0
2
0

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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?