0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

値は引き渡されているのにDBに格納されない(エラー解決はしてる)

Last updated at Posted at 2020-08-11

解決はしたけどわからない。。。。
今回のエラーはコメントの投稿が反映されませんでした。
下記コード

top_page>show.html.haml
.main
  = form_for @comment , url:{controller: 'responses', action: 'create'}do |f|
    .main__consultation
      .main__consultation__title
        お悩み一覧
      .main__consultation__text
        = @toppage.contents
    .main__comment
      = f.text_area :comment, class: "main__comment__form"
      = hidden_field_tag :top_page_id, value: @toppage.id
      .main__comment__sent
        = f.submit "投稿する"
      .main__comment__text
        <コメント一覧>
      -if @comments
        -@comments.each do |response|
          .main__comment__main
            .main__comment__main__name
              = response.user.name
            .main__comment__main__form
              = response.comment
responses_controller.rb
class ResponsesController < ApplicationController
  def create
    response = Response.create(response_params)
    redirect_to root_path
  end

  private
  def response_params
    params.require(:response).permit(:comment).merge(user_id: current_user.id, top_page_id: params[:top_page_id])
  end
end

responsesにcreate!に変更してエラーを吐かせてみました。
するとこんな感じ。
ace0635e27c1c6df1b452ecdf9a0c2b5.png

バリデーションのエラーってことですよね?
モデルは確認して大丈夫そうだったので
binding.pryで覗いてみました。
1c9114613ef1ee01671fd82269b64fe2.png
値はしっかり入ってます。
ただこの
"top_page_id"=>"{:value=>13}"
valueに妙に違和感を感じたので、試しに外したところ。。。

投稿できました。。。。。。

謎です。valueも値を返すのでは?
そもそもvalueをつけるときとつけない時の差って???

有識者の方教えて頂けるとありがたいです。。。。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?