質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

824閲覧

(ruby on rails) Routing Errorを解決したいです。

nijima

総合スコア27

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/07/20 01:20

本を投稿するサイトをつくっています。本を投稿するフォームをつくり、本を投稿できるようになったのですが、詳細画面から本を投稿しようとすると以下のようなエラーが発生します。詳細画面以外から本を投稿すると、エラーなく投稿に成功します。
このエラーの原因をご指導いただきたいです。何卒よろしくお願いいたします。イメージ説明

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

[POST] "/books/27" だと既存の book に対して POST してます

<%= form_with model: book, local: true do |f| %>
の model: book で指定されている book に入っているものが

作成画面?では@book=Book.new
詳細画面では@book = Book.find(params[:id])

になっているせいだと思います。

詳細画面のコントローラーでも @new_book=Book.new などを用意して

<%= render "books/form", book: @new_book %>

というように呼び出す必要があります。

投稿2021/07/20 01:46

anozon

総合スコア662

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nijima

2021/07/20 02:11

ご丁寧にご指導いただき、ありがとうございます。無事解決できました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問