質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1460閲覧

入力フォーム追加•削除機能でのエラー”undefined method `new_record?' for nil:NilClass”

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/07/20 13:34

編集2021/07/23 15:53

プログラミング初心者です。現在、RubyonRailsにてwebアプリ作成中です。

gem 'cocoon'

を利用し、入力フォームの追加・削除の実装をしているのですが下記エラーが出てしまい
解決できずにいるのでお力をお貸しいただけたらと思い質問させていただきます。

前提

親 User
子 Post・Item

エラーメッセージ

NoMethodError in Posts#new Showing /myapp/app/views/items/_items_fields.html.erb where line #33 raised: undefined method `new_record?' for nil:NilClass Extracted source (around line #33): 31    </div> 32    <!-- 削除ボタン --> 33    <%= link_to_remove_association "削除", f %> 34    <% end %> 35    </td> 36    </tr> Trace of template inclusion: #<ActionView::Template app/views/posts/new.html.erb locals=[]> Rails.root: /myapp Application Trace | Framework Trace | Full Trace app/views/items/_items_fields.html.erb:33 app/views/items/_items_fields.html.erb:7 app/views/posts/new.html.erb:15 app/views/posts/new.html.erb:14 app/views/posts/new.html.erb:5

該当のソースコード

html

1**post/new.html.erb** 2 3<div class="main posts-new"> 4 <div class="container"> 5 <h1 class="form-heading">投稿画面</h1> 6 7 <%= form_with(model: @user, local: true) do |s| %> 8 <p>メイン写真</p> 9 <label for="file_photo"> 10 <%= s.label :image_photo %> 11 <input name="image_photo" type="file" id="file_photo" > 12 </label> 13 <%= s.text_field :image_title %> 14 <%= s.text_field :description %> 15 <div class="parents"> 16 <%= s.fields_for :items do |t| %> 17 <%= render "items/items_fields", f: t %> 18 <% end %> 19 <div class="links"> 20 <%= link_to_add_association "追加", s, :items %> 21 </div> 22 </div> 23 <div class="actions"> 24 <input type="submit" value="投稿"> 25 </div> 26 <% end %> 27 28 <%= link_to("トップページへ戻る", "/") %> 29 </div> 30</div>

html

1**items/_item.fields.html.erb** 2 3<div class="nested-fields"> 4 <table> 5 <tr> 6 <td class="td-fixed-select"> 7 8 <%= f.fields_for :items do |t| %> 9 <div class="msr_file_05"> 10 <p>アイテム写真</p> 11 <label for="file_photo"> 12 画像を選択してください 13 <%= f.label :item_image %> 14 <input name="item_image" type="file" id="file_photo" > 15 </label> 16 </div> 17 <div class="msr_text_05"> 18 <label>商品名</label> 19 <%= f.text_field :products_name %> 20 </div> 21 <div class="msr_text_05"> 22 <label>ブランド</label> 23 <%= f.text_field :brand %> 24 </div> 25 <div class="msr_text_05"> 26 <label>サイズ</label> 27 <%= f.text_field :size %> 28 </div> 29 <div class="msr_text_05"> 30 <label>価格</label> 31 <%= f.text_field :price %> 32 </div> 33 <!-- 削除ボタン --> 34 <%= link_to_remove_association "削除", f %> 35 <% end %> 36 </td> 37 </tr> 38 </table> 39</div>

html

1**users_controller** 2 3class UsersController < ApplicationController 4 before_action :logged_in_user, only:[:destroy] 5 6 def index 7 @users = User.all 8 end 9 10 def show 11 @user = User.find_by(id: params[:id]) 12 end 13 14 def new 15 @user = User.new 16 @post = @user.post.build 17 @item = @post.item.build 18 end 19 20 def create 21 @user = User.new(user_params) 22 if @user.save 23 session[:user_id] = @user.id 24 flash[:notice] = "ユーザー登録が完了しました" 25 redirect_to("/users/#{@user.id}") 26 else 27 render("users/new") 28 end 29 end 30 31 def edit 32 @user = User.find_by(id: params[:id]) 33 end 34 35 def update 36 @user = User.find_by(id: params[:id]) 37 @user.name = params[:name] 38 @user.email = params[:email] 39 @user.password = params[:password] 40 if @user.save 41 flash[:notice] = "ユーザー情報を編集しました" 42 redirect_to("/users/#{@user.id}") 43 else 44 render("users/edit") 45 end 46 end 47 48 private 49def user_params 50 params.require(:user).permit(:name, :email, :password, post_attributes: [:id, :image_title, :description, item_attributes: [:id, :item_image, :products_name, :brand, :size, :price]]) 51end 52end

html

1**post_controller** 2class PostsController < ApplicationController 3 before_action :ensure_correct_user, {only: [:edit, :update, :destroy]} 4 def index 5 @posts = Post.all.order(created_at: :desc) 6 end 7 8 def show 9 @post = Post.find_by(id: params[:id]) 10 @user = @post.user 11 end 12 13 def new 14 @post = Post.new 15 end 16 17 def create 18 @post = Post.new(image_title: params[:image_title], description: params[:description],products_name: params[:products_name], brand: params[:brand], size: params[:size], price: params[:price]) 19 @post.user_id = @current_user.id 20 if @post.save 21 @post.image_photo="#{@post.id}.jpg" 22 image=params[:image_photo] 23 File.binwrite("app/assets/images/posts/#{@post.image_photo}", image.read) 24 redirect_to("/posts/#{@post.id}") 25 else 26 render("/posts/new") 27 end 28 29 30 end 31 32 def edit 33 @post = Post.find_by(id: params[:id]) 34 end 35 36 def update 37 @post = Post.find_by(id: params[:id]) 38 @post.image_title = params[:image_title] 39 @post.price = params[:price] 40 @post.image_photo="#{@post.id}.jpg" 41 image=params[:image_photo] 42 File.binwrite("app/assets/images/posts/#{@post.image_photo}", image.read) 43 if @post.save 44 redirect_to("/posts/#{@post.id}") 45 else 46 render("posts/#{@post.id}/edit") 47 end 48 end 49 50 def destroy 51 @post = Post.find_by(id: params[:id]) 52 @post.destroy 53 redirect_to("/") 54 end 55 56 def ensure_correct_user 57 @post = Post.find_by(id: params[:id]) 58 if @post.user_id != @current_user.id 59 flash[:notice] = "権限がありません" 60 redirect_to("/posts/index") 61 end 62 end 63end

html

1class Post < ApplicationRecord 2 belongs_to :user 3 has_many :items, dependent: :destroy 4 accepts_nested_attributes_for :items, allow_destroy: true 5 6 validates :user_id, {presence: true} 7 8 def user 9 return User.find_by(id: self.user_id) 10 end 11end

html

1class User < ApplicationRecord 2 has_secure_password 3 has_many :posts, dependent: :destroy 4 accepts_nested_attributes_for :posts, allow_destroy: true 5 6 7 validates :name, {presence: true} 8 validates :email, {presence: true, uniqueness: true} 9 10 def posts 11 return Post.where(user_id: self.id) 12 end 13end 14

undefined method `new_record?' for nil:NilClassとエラーがでてしまうのですが
消去ボタンである、<%= link_to_remove_association "削除", f %>の記述が間違っているまたは使い方が違うのでしょうか。

ご回答よろしくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

テーブル設計のミスによりエラーが生じていたようです。
最初から作り直し押したら動作しました。

投稿2021/07/26 11:31

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問