HARUNA3
@HARUNA3

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

error_messagesが読み込まれずNoMethodErrorになってしまう

解決したいこと

error文のrenderingがうまくいかず、NoMethodErrorが出てしまう。

furimaアプリの商品編集機能の実装中に、情報が不足している時にエラーが出る挙動を確認しようと思った所、error文がうまく出ずにNoMethodError となってしまった。

発生している問題・エラー

NoMethodError in Item#update
Showing /Users/ユーザー名/projects/furima/app/views/shared/_error_messages.html.erb where line #1 raised:
undefined method `errors' for nil:NilClass

エラー写真.png

エラー文
Started PATCH "/item/16" for ::1 at 2021-07-23 20:03:27 +0900
Processing by ItemController#update as HTML
  Parameters: {"authenticity_token"=>"kTsLM+Fz13dlesxP1p5i16GPC/A3gGNMVUZ7zMPZ7Eww7AV2M1I1K4bOP7L49Yoq1bfKAdPbqJngl5uYL12enw==", "item"=>{"name"=>"omuraisu", "item_info"=>"", "item_category_id"=>"2", "item_sales_status_id"=>"2", "item_shipping_fee_status_id"=>"2", "prefecture_id"=>"2", "item_scheduled_delivery_id"=>"2", "item_price"=>"10000"}, "commit"=>"変更する", "id"=>"16"}
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
  Item Load (0.2ms)  SELECT `items`.* FROM `items` WHERE `items`.`id` = 16 LIMIT 1
  ↳ app/controllers/item_controller.rb:33:in `update'
   (0.1ms)  BEGIN
  ↳ app/controllers/item_controller.rb:34:in `update'
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
  ↳ app/controllers/item_controller.rb:34:in `update'
  ActiveStorage::Attachment Load (0.2ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 16 AND `active_storage_attachments`.`record_type` = 'Item' AND `active_storage_attachments`.`name` = 'image' LIMIT 1
  ↳ app/controllers/item_controller.rb:34:in `update'
   (0.1ms)  ROLLBACK
  ↳ app/controllers/item_controller.rb:34:in `update'
  Rendering item/edit.html.erb within layouts/application
  Rendered shared/_error_messages.html.erb (Duration: 0.9ms | Allocations: 1384)
  Rendered item/edit.html.erb within layouts/application (Duration: 2.0ms | Allocations: 2171)
Completed 500 Internal Server Error in 12ms (ActiveRecord: 1.1ms | Allocations: 10480)



ActionView::Template::Error (undefined method `errors' for nil:NilClass):
    1: <% if model.errors.any? %>
    2: <div class="error-alert">
    3:   <ul>
    4:     <% model.errors.full_messages.each do |message| %>

該当するソースコード

config>routes.rb
Rails.application.routes.draw do
  devise_for :users
  root to: "item#index"
  resources :item
end
app>controllers>item_controller.rb
class ItemController < ApplicationController
  before_action :authenticate_user!, except: [:index, :show]
  before_action :set_item, only: [:show, :edit]
  before_action :move_to_index, except: [:index, :show]

  def index
    @items = Item.includes(:user).order('created_at DESC')
  end

  def new
    @item = Item.new
  end

  def create
    @item = Item.new(item_params)
    if @item.save
      redirect_to root_path
    else
      render :new
    end
  end

  def show
  end

  def edit
    unless current_user.id == @item.user_id
      redirect_to root_path 
    end
  end

  def update
    item = Item.find(params[:id])
    if item.update(item_params)
      redirect_to item_path
    else
      render :edit
    end
  end

  def destroy
    item = Item.find(params[:id])
    item.destroy
    redirect_to root_path
  end

  private

  def item_params
    params.require(:item).permit(:image, :name, :item_info, :item_category_id,
                                :item_sales_status_id, :item_shipping_fee_status_id,
                                :prefecture_id, :item_scheduled_delivery_id, :item_price).merge(user_id: current_user.id)
  end

  def set_item
    @item = Item.find(params[:id])
  end

  def move_to_index
    redirect_to root_path unless user_signed_in?
  end

end

ruby:app>views>item>edit.html.erb

<div class="items-sell-contents">
  <header class="items-sell-header">
    <%= link_to image_tag('furima-logo-color.png' , size: '185x50'), "/" %>
  </header>
  <div class="items-sell-main">
    <h2 class="items-sell-title">商品の情報を入力</h2>
    <%= form_with model: @item, local: true do |f| %>

    <%= render 'shared/error_messages', model: f.object %>

app>views>shared>_error_messages_html.erb
<% if model.errors.any? %>
<div class="error-alert">
  <ul>
    <% model.errors.full_messages.each do |message| %>
    <li class='error-message'><%= message %></li>
    <% end %>
  </ul>
</div>
<% end %>

自分で試したこと

  • 元々

    model: @model url:item_path(@item)

    このように書いていましたが、urlの指定の仕方が間違っていてエラーが起きているのかと思い、削除したが状況は変わりませんでした。

  • そもそもrenderの記入内容がおかしいのかと思いましたが、newページなどでも同じerror文のfileを仕様しているため、この内容がおかしいという事はないかと思います。

0

1Answer

Your answer might help someone💌