解決したいこと
Ruby on Railsで読んだ本の感想を投稿するwebアプリを作って![スクリーンショット
います。
他人の投稿詳細画面だけに他人のユーザ情報を表示させたいです。
そのほかの画面にはログインユーザの情報を表示するように設定しています。
発生している問題・エラー
特にエラーは発生していないが、どこをどう直したらいいか分からないです。
該当するソースコード
ruby
1class BooksController < ApplicationController 2 def create 3 @user = current_user 4 @books = Book.all 5 @book = Book.new(book_params) 6 @book_new = Book.new(book_params) 7 @book.user_id = current_user.id 8 if @book.save 9 redirect_to book_path(@book.id), notice: 'You have created book successfully.' 10 else 11 render :index 12 end 13 end 14 15 def index 16 @user = current_user 17 @users = User.all 18 @book = Book.new 19 @books = Book.all 20 @book_new = Book.new 21 end 22 23 def show 24 @user = current_user 25 @book = Book.find(params[:id]) 26 @book_new = Book.new 27 end 28: 29: 30: 31 private 32 def book_params 33 params.require(:book).permit(:title, :body) 34 end 35end
ruby
1class UsersController < ApplicationController 2 def index 3 @user = current_user 4 @users = User.all 5 @book_new = Book.new 6 end 7 8 def show 9 @user = current_user 10 @book =Book.new 11 @book_new = Book.new 12 @user = User.find(params[:id]) 13 @books = @user.books 14 end 15: 16: 17: 18 private 19 def user_params 20 params.require(:user).permit(:name, :profile_image, :introduction) 21 end 22end
投稿詳細画面(books/show)
ruby
1 2<%= render 'books/list', books: @books, user: @user %> 3<div class="col-md-8 offset-md-1"> 4<h2>Book detail</h2> 5<table class="table"> 6 <tbody> 7 <tr> 8 <td> 9 <%= link_to user_path(@user) do %> 10 <%= attachment_image_tag @book.user, :profile_image, :fill, 100, 100, fallback: "default-image.jpg" %> 11 <% end %> 12 <br><%= link_to @book.user.name, user_path(@user.id) %></td> 13 <td><%= link_to @book.title, book_path(@book.id) %></td> 14 <td><%= @book.body %></td> 15 <% if @book.user.id == current_user.id %> 16 <td><%= link_to "Edit", edit_book_path(@book), class: "btn btn-sm btn-success" %></td> 17 <td><%= link_to "Destroy", book_path(@book), method: :delete, class: "btn btn-sm btn-danger", "data-confirm" => "本当に消しますか?" %></td> 18 <% end %> 19 </tr> 20 </tbody> 21</table> 22</div> 23</div> 24</div>
booksの部分テンプレート(books_list)
ruby
1<p id="notice"><%= flash[:notice] %></p> 2 <div class='container px-5 px-sm-0'> 3 <div class='row'> 4 <div class='col-md-3'> 5 <h2>User info</h2> 6 <%= attachment_image_tag user, :profile_image, size: "100x100", fallback: "default-image.jpg" %> 7 <table class="table"> 8 <tbody> 9 <tr></tr> 10 <tr> 11 <th>name</th> 12 <th><%= user.name %></th> 13 </tr> 14 <tr> 15 <th>introduction</th> 16 <th><%= user.introduction %></th> 17 </tr> 18 </tbody> 19 </table> 20 <div class='row'> 21 <% if @user.id == current_user.id %> 22 <%= link_to edit_user_path(current_user.id), class: "btn btn-outline-secondary btn-block" do %> 23 <i class="fas fa-user-cog"></i> 24 <% end %> 25 <% end %> 26 </div> 27 28 <h2 class="mt-3">New book</h2> 29 <%= form_with model: @book_new, local: true do |f| %> 30 <div class = "form-group"> 31 <label for="book_title">Title</label> 32 <%= f.text_field :title, class: "form-control" %> 33 </div> 34 <div class = "form-group"> 35 <label for="book_opinion">Opinion</label> 36 <%= f.text_area :body, class: "form-control" %> 37 </div> 38 <div class = "form-group"> 39 <%= f.submit 'Create Book', class: "btn btn-success" %> 40 </div> 41 <% end %> 42 </div>
https://qiita.com/sarina-cat/questions/3bac956f96bb75bed784
以下ご対応ください。
https://teratail.com/help#posted-otherservice

回答2件
あなたの回答
tips
プレビュー