undefined method `new_record?' for nil:NilClassを解決したいです。
新規投稿画面と編集画面を部分テンプレートで作成し
submitボタンの表示する文字をファイルによって変えたいです。新規投稿時はCreateBook、編集の時はUpdateBookが表示されるようにしたいです。
その際あるサイト(https://teratail.com/questions/312875)を参考に
_form.html.erb
RubyonRails
1<% if f.object.new_record? %> 2 <%= f.submit "Create Book", class: "btn btn-success" %> 3 <% else %> 4 <%= f.submit "Update Book", class: "btn btn-success" %> 5 <% end %>
を記述した所、userのshow.html.erb(userの詳細ページ)でエラーが発生しました
(サイトにあるようにnew_record?メソッドを自分で定義する必要は無いと思います)
(新規投稿のページや投稿を編集するページではエラーは発生していません)
発生している問題・エラーメッセージ
NoMethodError in Users#show
Showing /home/ec2-user/environment/bookers2-debug/app/views/books/_form.html.erb where line #21 raised:
undefined method `new_record?' for nil:NilClass
Extracted source (around line #21):
19
20
21
22
23
24
<% if f.object.new_record? %> <%= f.submit "Create Book", class: "btn btn-success" %> <% else %> <%= f.submit "Update Book", class: "btn btn-success" %>
Trace of template inclusion: app/views/users/show.html.erb
Rails.root: /home/ec2-user/environment/bookers2-debug
Application Trace | Framework Trace | Full Trace
app/views/books/_form.html.erb:21:in block in _app_views_books__form_html_erb__3555135995817864763_70203137971340' app/views/books/_form.html.erb:1:in
_app_views_books__form_html_erb__3555135995817864763_70203137971340'
app/views/users/show.html.erb:8:in `_app_views_users_show_html_erb___123336745112956188_70203137756200'
Request
Parameters:
{"id"=>"1"}
Toggle session dump
Toggle env dump
Response
Headers:
None
該当のソースコード
books/_form.html.erb
RubyonRails
1<%= form_with model:book,local:true do |f| %> 2 <div class="form-group"> 3 <%= f.label :title %> 4 <%= f.text_field :title, class: 'form-control book_title' %> 5 </div> 6 <div class="form-group"> 7 <%= f.label :opinion %> 8 <%= f.text_area :body, class: 'form-control book_body' %> 9 </div> 10 <div class="form-group"> 11 <% if f.object.new_record? %> 12 <%= f.submit "Create Book", class: "btn btn-success" %> 13 <% else %> 14 <%= f.submit "Update Book", class: "btn btn-success" %> 15 <% end %> 16 </div> 17<% end %>
user/show.html.erb
RubyonRails
1<div class='container px-5 px-sm-0'> 2 <div class='row'> 3 <div class='col-md-3'> 4 <h2>User info</h2> 5 <%= render 'info', user: @user %> 6 <h2 class="mt-3">New book</h2> 7 <%= render 'books/form', book: @book_form %> 8 </div> 9 <div class='col-md-8 offset-md-1'> 10 <h2>Books</h2> 11 <%= render 'books/index',books: @books %> 12 </div> 13 </div> 14</div>
books_controller.rb
RubyonRails
1class BooksController < ApplicationController 2before_action :correct_user, only: [:edit, :update] 3def show 4 @book_form = Book.new 5 @book = Book.find(params[:id]) 6 @user = @book.user 7 end 8 9 def index 10 @books = Book.all 11 @book = Book.new 12 13 end 14 15 def create 16 @book = Book.new(book_params) 17 @book.user_id = current_user.id 18 if @book.save 19 redirect_to book_path(@book), notice: "You have created book successfully." 20 else 21 @books = Book.all 22 render 'index' 23 end 24 end 25 26 def edit 27 @book = Book.find(params[:id]) 28 29 end 30 31 32 33 def update 34 @book = Book.find(params[:id]) 35 if @book.update(book_params) 36 redirect_to book_path(@book), notice: "You have updated book successfully." 37 else 38 render "edit" 39 end 40 end 41 42 def destroy 43 @book = Book.find(params[:id]) 44 @book.destroy 45 redirect_to books_path 46 end 47 48 private 49 50 def book_params 51 params.require(:book).permit(:title, :body) 52 end 53 54 def correct_user 55 @book = Book.find(params[:id]) 56 @user = @book.user 57 redirect_to(books_path) unless @user == current_user 58 end 59 60end 61
users_controller.rb
RubyonRails
1class UsersController < ApplicationController 2 3 4 before_action :ensure_correct_user, only: [:update, :edit] 5 6 7 8 def show 9 @user = User.find(params[:id]) 10 @books = @user.books 11 @book = Book.new 12 13 end 14 15 def index 16 @users = User.all 17 @book = Book.new 18 19 end 20 21 def edit 22 @user = User.find(params[:id]) 23 end 24 25 def update 26 if @user.update(user_params) 27 redirect_to user_path(@user), notice: "You have updated user successfully." 28 else 29 @user = User.find(params[:id]) 30 @books = @user.books 31 @book = Book.new 32 render "show" 33 end 34 end 35 36 private 37 def user_params 38 params.require(:user).permit(:name, :introduction, :profile_image) 39 end 40 41 42 def ensure_correct_user 43 @user = User.find(params[:id]) 44 unless @user == current_user 45 redirect_to user_path(current_user) 46 end 47 end 48 49 end 50
models/book.rb
RubyonRails
1class Book < ApplicationRecord 2 belongs_to :user 3 4 validates :title, presence: true 5 validates :body, presence: true, length: {maximum: 200} 6end 7
models/user.rb
RubyonRails
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 has_many :books 8 attachment :profile_image, destroy: false 9 10 validates :name, length: {maximum: 20, minimum: 2}, uniqueness: true 11 validates :introduction, length: {maximum: 50} 12 13end 14
試したこと
自分で調べた所、Rubyのnew_record?メソッドを使うことはわかったが
使い方まではわからなかった
エラー文よりobjectが空だと思い、
objectの部分をbookにしたが
解決には至らなかった
gemをインストールするのかと思い、gem new_record?で調べたが
解決には至らなかった
補足情報(FW/ツールのバージョンなど)
バージョン
Rails 5.2.5
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/11/18 10:48