フリマアプリにて商品投稿機能を実装 NNoMethodError in Items#newについて

前提

フリマアプリにて商品投稿を実装
投稿ページまで表示させることができたのですが、
ビューのformで持ってきたい情報を入力するとエラーにかかり1日苦戦しています。
エラー箇所をモデル名を外した場合はビューは反映されているのでform書き方か
値を持ってこれていないと仮説を立てました。
エラーより、undefined method `key?' for nil:NilClass
keyが定義されていない?呼び出そうとした?けど定義されていないというところから
ルーティング、MVCを確認しバリデーションも問題なさそうと判断しましたが未だに原因特定できず
お知恵をおかり出来れば幸いです。

素人で

発生している問題・エラーメッセージ

NoMethodError in Items#new Showing /Users/admin/projects/furima-38877/app/views/items/new.html.erb where line #9 raised: undefined method `key?' for nil:NilClass Extracted source (around line #9): <h2 class="items-sell-title">商品の情報を入力</h2> <%# <%= form_with local: true do |f| %> %> <%= form_with(model: @item, local: true ) do |f| %> <%# インスタンスを渡して、エラー発生時にメッセージが表示されるようにしましょう。%> <%# render 'shared/error_messages', model: f.object %>

該当のソースコード

**controller** def index @item = Item.all end def new @item = Item.new @item = Category.all @item = Postage.all @item = Condition.all @item = Prefecture.all @item = Preparation.all end def create @item = Item.new(item_params) if @item.save redirect_to root_path else render :new end end private def item_params params.require(:item).permit(:image, :item_name, :explanation, :category_id, :condition_id, :postage_id, :prefecture_id, :preparation_id, :price).merge(user_id: current_user.id) end end
**model** belongs_to :user has_one_attached :image # has_one :order extend ActiveHash::Associations::ActiveRecordExtensions belongs_to :category belongs_to :condition belongs_to :postage belongs_to :prefecture belongs_to :preparation with_options presence: true do validates :image validates :item_name validates :explanation validates :price, numericality: { greater_than_or_equal_to: 300, less_than_or_equal_to: 9_999_999 } # カテゴリー validates :category_id,numericality: { other_than: 1 , message: "can't be blank"} # 商品の状態 validates :condition_id,numericality: { other_than: 1 , message: "can't be blank"} # 配送料の負担 validates :postage_id,numericality: { other_than: 1 , message: "can't be blank"} # 都道府県 validates :prefecture_id,numericality: { other_than: 1 , message: "can't be blank"} # 発送までの日数 validates :preparation_id,numericality: { other_than: 1 , message: "can't be blank"} end
**view** <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 local: true do |f| %> %> <%= form_with(model: @item, local: true ) do |f| %> <%# render 'shared/error_messages', model: f.object %>

試したこと

バリデーションに相違がないか、
ルーティングの設定はされているか。
パラムスの値に相違がないか。
binding.pryを試しましたがnillとなり、どこを直せばよいのか分からなくなってしまいました。。。

コメントを投稿

0 コメント