前提
・本のタイトルと感想を投稿するアプリケーションをRailsで作成しています。
・投稿・削除・編集などの機能は実装確認済み。
※記憶違いでなければ,確か共通テンプレート化する前にUserのshow.html.erbにBookの記述を貼り付けてみたら正常に実装できていた...ような(昨日のことと、ずっとエラーでいろいろ試していたのでもしかしたらカリキュラムの完成品と見比べていたので記憶が混ざっている可能性がある)
実現したいこと
・エラーの解決
・共通テンプレート化を行い、UserのビューShowページにBookのビューであるIndex,Newを表示させたい。
発生している問題・エラーメッセージ
ruby
1ActionView::MissingTemplate in Users#show 2Showing /home/ec2-user/environment/bookers2/app/views/users/show.html.erb where line #9 raised: 3 4Missing partial app/views/users/__user with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in: 5 * "/home/ec2-user/environment/bookers2/app/views" 6 * "/home/ec2-user/.rvm/gems/ruby-3.1.2/gems/devise-4.8.1/app/views" 7 * "/home/ec2-user/.rvm/gems/ruby-3.1.2/gems/actiontext-6.1.6.1/app/views" 8 * "/home/ec2-user/.rvm/gems/ruby-3.1.2/gems/actionmailbox-6.1.6.1/app/views" 9Extracted source (around line #9): 10 11 <p><%= link_to "プロフィール編集", edit_user_path(@user) %></p> 12 </div> 13 <%= render 'app/views/users/_user', books: @books %> 14</main>
該当のソースコード
ruby
1<main> 2 <div> 3 <h1>User info</h1> 4 <%= image_tag @user.get_profile_image(100,100) %><br> 5 <h3>name <%= @user.name %></h3> 6 <h3>introduction <%= @user.user_introduction %></h3> 7 <p><%= link_to "プロフィール編集", edit_user_path(@user) %></p> 8 </div> 9 <%= render 'app/views/users/_user', books: @books %> 10</main>
試したこと
・アソシエーションができているか確認もしてみましたが、多分問題ない?と思います。
・ファイルの指定も確認して記述し直しましたがエラー解決できませんでした。
補足情報(FW/ツールのバージョンなど)
Ruby3.1.2
ここにより詳細な情報を記載してください。
ruby(model)
1class User < ApplicationRecord 2: 3 has_many :books, dependent: :destroy 4 has_one_attached :profile_image 5: 6end 7 8class Book < ApplicationRecord 9 belongs_to :user 10end
ruby(books_controller)
1class BooksController < ApplicationController 2 def new 3 @book = Book.new(book_paarams) 4 end 5 6 def create 7 @book = Book.new(book_params) 8 @book.user_id = current_user.id 9 @book.save 10 redirect_to books_path(@book.id) 11 end 12 13 def index 14 @books = Book.all 15 @user = @books 16 end 17 18 def show 19 @book = Book.find(params[:id]) 20 @user = @book 21 end 22 23 def edit 24 @book = Book.find(params[:id]) 25 end 26 27 def destroy 28 @book = Book.find(params[:id]) 29 @book.destroy 30 redirect_to book_path 31 end 32 33 private 34 35 def book_params 36 params.require(:book).permit(:title, :body, :id, ) 37 end 38end
ruby(users_controller)
1class UsersController < ApplicationController 2 before_action :correct_user, only: [:edit, :update] 3 def show 4 @user = User.find(params[:id]) 5 @books = @user.books 6 end 7 8 def edit 9 @user = User.find(params[:id]) 10 end 11 12 def index 13 @users = User.all 14 end 15 16 def update 17 @user = User.find(params[:id]) 18 @user.update(user_params) 19 redirect_to user_path 20 end 21 22 private 23 24 def correct_user 25 user = User.find(params[:id]) 26 if current_user_id != user.id 27 redirect_to user_path(current_user.id) 28 end 29 end 30 def user_params 31 params.require(:user).permit(:name, :profile_image, :user_introduction) 32 end 33 34end 35
ruby(route)
1Rails.application.routes.draw do 2 get 'homes/about', to: 'homes#show', as: :about 3 post 'books/book.id' => 'books#create' 4 devise_for :users 5 root to: "homes#top" 6 resources :books, only: [:show, :edit, :index, :create, :new, :destroy] 7 resources :users, only: [:show, :edit, :index, :update, :create,] 8end
ruby(users/show.html.erb)
1<main> 2 <div> 3 <h1>User info</h1> 4 <%= image_tag @user.get_profile_image(100,100) %><br> 5 <h3>name <%= @user.name %></h3> 6 <h3>introduction <%= @user.user_introduction %></h3> 7 <p><%= link_to "プロフィール編集", edit_user_path(@user) %></p> 8 </div> 9 <%= render 'user', books: @books %> 10</main>
ruby(books/new.html.erb,books/index.html.erb)
1<%= render 'users/user', books: @books %>
ruby(_user.html.erb)
1<div> 2 <h2>New book</h2> 3 <%= form_with model: books do |f| %> 4 <p>Title</p><%= f.text_field :title %> 5 <p>Opinion</p><%= f.text_area :body %> 6 <br><%= f.submit 'Create Book' %> 7 <% end %> 8 </div> 9 10 <div> 11 <h1>Books</h1> 12 <%= books.each do |book| %> 13 <%= link_to user_path(book.user.id) do %> 14 <%= image_tag book.user.get_profile_image(100,100) %> 15 <% end %><br> 16 <h3>Title</h3><br><%= link_to book.title, book_path(book.id) %><br> 17 <h3>Opinion</h3><br><%= book.body %><br> 18 <% end %> 19 </div>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/08 02:03
2022/08/08 09:08
2022/08/08 12:01
2022/08/08 13:00
2022/08/08 20:30
2022/08/09 14:30