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

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

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

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

jQuery

jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

Q&A

解決済

1回答

902閲覧

Ratyを使用したレビュー投稿でrateカラムの保存ができない

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby

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

jQuery

jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

0グッド

0クリップ

投稿2021/12/14 05:46

###Ratyを使用したレビュー投稿でrateカラムの保存ができない

現在RubyonRailsを用いて本の投稿アプリを作成しています。
一通り作成した後で、Ratyでの星評価機能を追加している状態です。

新規投稿後の詳細画面で星評価が表示されず困っています。

以下該当のソースコードです。

####booksコントローラー

ruby

1class BooksController < ApplicationController 2 before_action :ensure_correct_user, only: [:edit,:update,:destroy] 3 4 def show 5 @book = Book.find(params[:id]) 6 @book_comment = BookComment.new 7 end 8 9 def index 10 @books = Book.all 11 @book = Book.new 12 end 13 14 def create 15 @book = Book.new(book_params) 16 @book.user_id = current_user.id 17 if @book.save 18 redirect_to book_path(@book), notice: "You have created book successfully." 19 else 20 @books = Book.all 21 render 'index' 22 end 23 end 24 25 def edit 26 end 27 28 def update 29 if @book.update(book_params) 30 redirect_to book_path(@book), notice: "You have updated book successfully." 31 else 32 render "edit" 33 end 34 end 35 36 def destroy 37 @book.destroy 38 redirect_to books_path 39 end 40 41 private 42 43 def book_params 44 params.require(:book).permit(:title,:body) 45 end 46 47 def ensure_correct_user 48 @book = Book.find(params[:id]) 49 unless @book.user_id == current_user.id 50 redirect_to books_path 51 end 52 end 53end

####schemaファイル

ruby

1# This file is auto-generated from the current state of the database. Instead 2# of editing this file, please use the migrations feature of Active Record to 3# incrementally modify your database, and then regenerate this schema definition. 4# 5# Note that this schema.rb definition is the authoritative source for your 6# database schema. If you need to create the application database on another 7# system, you should be using db:schema:load, not running all the migrations 8# from scratch. The latter is a flawed and unsustainable approach (the more migrations 9# you'll amass, the slower it'll run and the greater likelihood for issues). 10# 11# It's strongly recommended that you check this file into your version control system. 12 13ActiveRecord::Schema.define(version: 2021_12_14_040244) do 14 15 create_table "book_comments", force: :cascade do |t| 16 t.integer "book_id" 17 t.integer "user_id" 18 t.text "comment" 19 t.datetime "created_at", null: false 20 t.datetime "updated_at", null: false 21 end 22 23 create_table "books", force: :cascade do |t| 24 t.string "title" 25 t.text "body" 26 t.integer "user_id" 27 t.datetime "created_at", null: false 28 t.datetime "updated_at", null: false 29 t.float "rate" 30 end 31 32 create_table "favorites", force: :cascade do |t| 33 t.integer "user_id" 34 t.integer "book_id" 35 t.datetime "created_at", null: false 36 t.datetime "updated_at", null: false 37 end 38 39 create_table "relationships", force: :cascade do |t| 40 t.integer "follower_id" 41 t.integer "followed_id" 42 t.datetime "created_at", null: false 43 t.datetime "updated_at", null: false 44 end 45 46 create_table "users", force: :cascade do |t| 47 t.string "email", default: "", null: false 48 t.string "encrypted_password", default: "", null: false 49 t.string "reset_password_token" 50 t.datetime "reset_password_sent_at" 51 t.datetime "remember_created_at" 52 t.string "name" 53 t.text "introduction" 54 t.string "profile_image_id" 55 t.datetime "created_at", null: false 56 t.datetime "updated_at", null: false 57 t.index ["email"], name: "index_users_on_email", unique: true 58 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 59 end 60 61end 62

####投稿フォーム

html

1app/views/book/_form.html.erb 2<%= form_with model:book,local: true do |f| %> 3 <div class="form-group"> 4 <%= f.label :title %> 5 <%= f.text_field :title, class: 'form-control book_title' %> 6 </div> 7 <div class="form-group"> 8 <%= f.label :opinion %> 9 <%= f.text_area :body, class: 'form-control book_body' %> 10 </div> 11 <div class="form-group" id="star"> 12 <%= f.label :rate %> 13 <%= f.hidden_field :rate, id: :review_star %> 14 </div> 15 <script> 16 $('#star').raty({ 17 size: 36, 18 starOff: '<%= asset_path('star-off.png') %>', 19 starOn: '<%= asset_path('star-on.png') %>', 20 starHalf: '<%= asset_path('star-half.png') %>', 21 scoreName: 'book[rate]', 22 half: true, 23 }); 24 </script> 25 <div class="form-group"> 26 <%= f.submit class: 'btn btn-success' %> 27 </div> 28<% end %>

####投稿詳細ページ

html

1<div class='container'> 2 <div class='row'> 3 <div class='col-md-3'> 4 <h2>User info</h2> 5 <%= render 'users/info', user: @book.user %> 6 <h2 class="mt-3">New book</h2> 7 <%= render 'books/form', book: Book.new %> 8 </div> 9 <div class='col-md-8 offset-md-1'> 10 <h2>Book detail</h2> 11 <table class='table'> 12 <tr id="book_<%= @book.id %>"> 13 <td><%= link_to user_path(@book.user) do %> 14 <%= attachment_image_tag(@book.user, :profile_image, :fill, 100, 100, fallback: "no-image-icon.jpg") %><br> 15 <%= @book.user.name %> 16 <% end %> 17 </td> 18 <td><%= link_to @book.title, book_path(@book) %></td> 19 <td><%= @book.body %></td> 20 <td class="favorite-btn"><%= render "favorites/favorite-btn",book: @book %></td> 21 <td class="comment-count"><p>コメント数: <%= @book.book_comments.count %></p></td> 22 <td id="star-rate-<%= @book.id %>"></td> 23 <script> 24 $('#star-rate-<%= @book.id %>').raty({ 25 size: 36, 26 starOff: '<%= asset_path('star-off.png') %>', 27 starOn: '<%= asset_path('star-on.png') %>', 28 starHalf: '<%= asset_path('star-half.png') %>', 29 half: true, 30 readOnly: true, 31 score: <%= @book.rate %>, 32 }); 33 </script> 34 <% if @book.user == current_user %> 35 <td><%= link_to 'Edit', edit_book_path(@book), class: "btn btn-sm btn-success edit_book_#{@book.id}" %></td> 36 <td><%= link_to 'Destroy', book_path(@book), method: :delete, data: { confirm: '本当に消しますか?' }, class: "btn btn-sm btn-danger destroy_book_#{@book.id}"%></td> 37 <% end %> 38 </tr> 39 </table> 40 <table class="book_comments"> 41 <%= render "book_comments/index",book: @book %> 42 </table> 43 <%= form_with model:[@book,@book_comment], remote:true do |f| %> 44 <%= f.text_area :comment,size: "90x5" %><br> 45 <%= f.submit "送信" %> 46 <% end %> 47 </div> 48 </div> 49</div>

試したこと

pry-byebugを使用し投稿後の@bookを確認してみたところ、rateカラムがnilで保存されていないことがわかりました。
しかしテーブル内ではfloat型として定義しており、
投稿フォーム内のscriptタグ内ではscoreName: 'book[rate]',と記述しているので、どこに問題があるかわからないです。

ご教授のほど、よろしくお願いいたします。

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

こちらの記事を実装時に参考させていただきました。
参考記事

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

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

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

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

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

guest

回答1

0

ベストアンサー

book_params に rate がありません

投稿2021/12/15 04:41

winterboum

総合スコア23347

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

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

退会済みユーザー

退会済みユーザー

2021/12/15 11:32

とても初歩的なミスでした...。 お手間おかけしてすみません。 解決いただいてありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問