本を投稿するサイトをつくっています。本を投稿するフォームをつくり、本を投稿できるようになったのですが、詳細画面から本を投稿しようとすると以下のようなエラーが発生します。詳細画面以外から本を投稿すると、エラーなく投稿に成功します。
このエラーの原因をご指導いただきたいです。何卒よろしくお願いいたします。
config/routes.rb Rails.application.routes.draw do devise_for :users root to: 'homes#top' get "home/about" => "homes#about" resources :books, only: [:edit, :create, :index, :show, :destroy, :update] resources :users, only: [:show, :edit, :update, :index] end
_form.html.erb <h2 class="mt-3">New book</h2> <%= form_with model: book, local: true do |f| %> <div class="form-group"> <%= f.label :Title %> <%= f.text_field :title, class:"form-control" %> </div> <div class="form-group"> <%= f.label :Opinion %> <%= f.text_area :body, class:"form-control" %> </div> <%= f.submit "Create Book", class:"btn btn-success" %> <% end %>
show.html.erb <div class="container mt-3"> <%= render "books/error", model: @user %> <div class="row"> <div class="col-md-3"> <%= render "users/info", user: @user %> <%= render "books/form", book: @book %> </div> <div class="col-md-8 offset-md-1"> <h2>Books</h2> <table class="table table-hover table-inverse"> <thead> <th></th> <th>Title</th> <th>Opinion</th> <th colspan="3"></th> </thead> <tbody> <% @books.each do |book| %> <tr> <td> <%= link_to user_path(@user) do %> <%= attachment_image_tag @user, :profile_image, format: "jpeg", fallback: "no_image.jpg", size: "40x40" %> <% end %> </td> <td> <%= link_to book.title, book_path(book) %> </td> <td><%= book.body %></td> </tr> <% end %> </tbody> </table> </div> </div> </div>
books_controller.rb class BooksController < ApplicationController def create @books = Book.all @book=Book.new(book_params) @book.user_id= current_user.id if @book.save redirect_to book_path(@book.id),notice: "You have created book successfully." else @user = current_user render :index end end def index @book=Book.new @books=Book.all @book.user_id = current_user.id @user = current_user end def edit @book = Book.find(params[:id]) if @book.user_id != current_user.id redirect_to books_path end end def update @book=Book.find(params[:id]) if @book.update(book_params) redirect_to book_path(@book.id), notice: "You have updated book successfully." else render :edit end end def show @book = Book.find(params[:id]) @user = User.find(current_user.id) @user = @book.user end def destroy @books = Book.find(params[:id]) @books.destroy redirect_to books_path end private def book_params params.require(:book).permit(:title, :body, :category) end end
users_controller.rb class UsersController < ApplicationController def show @user = User.find(params[:id]) @book=Book.new @books=@user.books end def index @users = User.all @user = current_user @book=Book.new end def edit @user = User.find(params[:id]) if @user == current_user render :edit else redirect_to user_path(current_user) end end def update @user = User.find(params[:id]) if @user.update(user_params) redirect_to user_path(@user.id) ,notice: "You have updated user successfully." else render :edit end end private def user_params params.require(:user).permit(:name, :profile_image, :introduction ) end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/07/20 02:11