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

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

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

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

Q&A

解決済

1回答

4750閲覧

rails 投稿時にフォームに入力した値が残ってしまう。

dragonnext

総合スコア3

Ruby

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

0グッド

0クリップ

投稿2020/08/05 07:09

前提・実現したいこと

railsで本の投稿アプリを制作しているのですが、新規投稿、ログインユーザー情報、投稿一覧が同じページにあり、新規投稿をすると詳細ページに飛ぶことはできるのですが、入力フォームに値が残ってしまいます。
投稿が完了したらフォーム内は空欄にしたいです。

発生している問題・エラーメッセージ

エラーメッセージ

該当のソースコード

コントローラーのソースコード

class BooksController < ApplicationController

def top end def index @book = Book.new @books = Book.all @user = current_user end def create @book = Book.new(book_params) @book.user_id = current_user.id if @book.save flash[:notice] = "You have created book successfully" redirect_to book_path(@book.id) else @user = current_user @books = Book.all render :index end end def show @book = Book.find(params[:id]) end def edit @book = Book.find(params[:id]) end def update @book = Book.find(params[:id]) if @book.update(book_params) flash[:notice] = "You have updated book successfully" redirect_to book_path(@book) else render :edit end 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

詳細画面のコード

<div class="container"> <div class="row"> <div class="col-md-3">

<% if flash[:notice] %>
<div class="flash">
<%= flash[:notice] %>
</div>
<% end %>

<h1>User info</h1>
<table class="table table-striped"> <thead> <tr> <th> <%= attachment_image_tag @book.user, :profile_image, :fill, 60, 60, format: 'jpeg', class: "img-square pull-left profile-img", fallback: "no_image.jpg" %> </th> <th></th> </tr> </thead> <tbody> <tr> <td>name</td> <td><%= @book.user.name%></td> </tr> <tr> <td>introduction</td> <td><%= @book.user.introduction %></td> </tr> </tbody> </table>

<% if @book.user == current_user %>
<%= link_to edit_user_path(current_user.id), class: "btn btn-default btn-block" do %>
<span class="glyphicon glyphicon-wrench" aria-hidden="true"></span>
<% end %>
<% else %>
<%= link_to edit_user_path(current_user), class: "btn btn-default btn-block" do %>
<span class="glyphicon glyphicon-wrench" aria-hidden="true"></span>
<% end %>
<% end%>

<h1>New book</h1>

<%= form_for(@book) do |f| %>

<h3>Title</h3> <%= f.text_field :title %> <h3>Opinion</h3> <%= f.text_area :body %><br> <%= f.submit 'Create Book' %>

<% end %>

</div> <div class="col-md-9"> <h1>Book detail</h1> <table class="table table-striped"> <thead> <tr> <th></th> <th></th> <th></th> <th></th> <th></th> </tr> </thead> <tbody> <tr> <td> <%= attachment_image_tag @book.user, :profile_image, :fill, 60, 60, format: 'jpeg', class: "img-square pull-left profile-img", fallback: "no_image.jpg", size: '60x60' %> <%= link_to user_path(@book.user.id) do %> <%= @book.user.name %> <% end %> </td> <td><%= @book.title %></td> <td><%= @book.body %></td> <td> <% if @book.user == current_user %> <%= link_to 'Edit', edit_book_path(@book.id), class: "btn btn-success" %> <% end %> </td> <td> <% if @book.user == current_user %> <%= link_to 'destroy', book_path(@book.id), method: :delete, data: { confirm: "本当に消しますか?"} , class: "btn btn-danger" %> <% end %> </td> </tr> </tbody> </table> </div> </div> </div>

試したこと

showアクションに@book=book.newを定義してみたのですが、そうするとshowページで必要な@book=Book.find(params[:id])が機能しなくなってしまいエラーが出てしまいました。
アドバイスいただけるとありがたいです。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

<%= form_for(@book) do |f| %>

erb

1<%= form_for(Book.new) do |f| %>

に変更するとよい。

もしくは、新しく@new_book = Book.newみたいな変数を作るのも悪くないかな

投稿2020/08/05 07:13

asm

総合スコア15147

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

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

dragonnext

2020/08/06 07:51

回答ありがとうございます。 アドバイス通り変数を新しく定義したら問題なく表示できました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問