ルートの設定で、/home/aboutのurlにbookモデルのabout.html.erbを紐付けたのですが、うまく行きません。
rails routesでルートを確認してみると特に問題なくurlも紐付いています。どう解決すれば良いかわかりません。お忙しいとは思いますが、回答よろしくお願いします。
book.contller.rb
class BooksController < ApplicationController def index @books = Book.all @user = current_user end def show @book = Book.find(params[:id]) @user = current_user end def new end def about end def create @user = current_user @books = Book.all @book = Book.new(book_params) @book.user_id = current_user.id @book.save if @book.save redirect_to book_path(@book.id) else redirect_to user_path(@user.id) end end def edit @book = Book.find(params[:id]) end def update @book = Book.find(params[:id]) @book.update(book_params) redirect_to book_path(@book.id) end def destroy @book = Book.find(params[:id]) @book.destroy redirect_to books_path end private def book_params params.require(:book).permit(:title, :body,) end end
application.html.erb
<!DOCTYPE html> <html> <head> <title>BookApp2</title> <%= csrf_meta_tags %> <%= csp_meta_tag %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> </head> <body> <div class="header-title"> <h3>Bookers</h3> <div class="header-list"> <% link_to "book", "/books" %> </div> <div class="header-item"> <% if user_signed_in? %> <p> <%= link_to "ログアウト", destroy_user_session_path, method: :delete %> </p> <% else %> <div class="header-top"> <%= link_to 'Home',root_path %> </div> <div class="header-home"> <%= link_to "about",about_book_path %> </div> <p> <%= link_to "sign up", new_user_registration_path %> </p> <p> <%= link_to "login", new_user_session_path %> </p> <% end %> </div> </div> <%= yield %> </body> </html>
routes.rb
Rails.application.routes.draw do get 'books/about' resources :books, only: [:new, :create, :index, :show, :destroy] devise_for :users, controllers: { sessions: 'users/sessions', registrations: 'users/registrations', } # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html root to:'homes#top' get 'home/about' => 'books#about' get 'books' => 'books#index' post 'books' => 'books#create' get 'books/:id/edit' => 'books#edit', as: 'edit_book' patch 'books/:id' => 'books#update', as: 'update_book' resources :users, only: [:show, :edit, :update] end
about.html.erb
<h1>Books#about</h1> <p>Find me in app/views/books/about.html.erb</p>
試したこと
・エラー文の確認
・コードの確認
回答よろしくお願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/28 02:05
2021/02/28 03:58
2021/02/28 03:59
2021/02/28 04:26