###Ratyを使用したレビュー投稿でrateカラムの保存ができない
現在RubyonRailsを用いて本の投稿アプリを作成しています。
一通り作成した後で、Ratyでの星評価機能を追加している状態です。
新規投稿後の詳細画面で星評価が表示されず困っています。
以下該当のソースコードです。
####booksコントローラー
ruby
class BooksController < ApplicationController before_action :ensure_correct_user, only: [:edit,:update,:destroy] def show @book = Book.find(params[:id]) @book_comment = BookComment.new end def index @books = Book.all @book = Book.new end def create @book = Book.new(book_params) @book.user_id = current_user.id if @book.save redirect_to book_path(@book), notice: "You have created book successfully." else @books = Book.all render 'index' end end def edit end def update if @book.update(book_params) redirect_to book_path(@book), notice: "You have updated book successfully." else render "edit" end end def destroy @book.destroy redirect_to books_path end private def book_params params.require(:book).permit(:title,:body) end def ensure_correct_user @book = Book.find(params[:id]) unless @book.user_id == current_user.id redirect_to books_path end end end
####schemaファイル
ruby
# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2021_12_14_040244) do create_table "book_comments", force: :cascade do |t| t.integer "book_id" t.integer "user_id" t.text "comment" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "books", force: :cascade do |t| t.string "title" t.text "body" t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.float "rate" end create_table "favorites", force: :cascade do |t| t.integer "user_id" t.integer "book_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "relationships", force: :cascade do |t| t.integer "follower_id" t.integer "followed_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.string "name" t.text "introduction" t.string "profile_image_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end end
####投稿フォーム
html
app/views/book/_form.html.erb <%= form_with model:book,local: true do |f| %> <div class="form-group"> <%= f.label :title %> <%= f.text_field :title, class: 'form-control book_title' %> </div> <div class="form-group"> <%= f.label :opinion %> <%= f.text_area :body, class: 'form-control book_body' %> </div> <div class="form-group" id="star"> <%= f.label :rate %> <%= f.hidden_field :rate, id: :review_star %> </div> <script> $('#star').raty({ size: 36, starOff: '<%= asset_path('star-off.png') %>', starOn: '<%= asset_path('star-on.png') %>', starHalf: '<%= asset_path('star-half.png') %>', scoreName: 'book[rate]', half: true, }); </script> <div class="form-group"> <%= f.submit class: 'btn btn-success' %> </div> <% end %>
####投稿詳細ページ
html
<div class='container'> <div class='row'> <div class='col-md-3'> <h2>User info</h2> <%= render 'users/info', user: @book.user %> <h2 class="mt-3">New book</h2> <%= render 'books/form', book: Book.new %> </div> <div class='col-md-8 offset-md-1'> <h2>Book detail</h2> <table class='table'> <tr id="book_<%= @book.id %>"> <td><%= link_to user_path(@book.user) do %> <%= attachment_image_tag(@book.user, :profile_image, :fill, 100, 100, fallback: "no-image-icon.jpg") %><br> <%= @book.user.name %> <% end %> </td> <td><%= link_to @book.title, book_path(@book) %></td> <td><%= @book.body %></td> <td class="favorite-btn"><%= render "favorites/favorite-btn",book: @book %></td> <td class="comment-count"><p>コメント数: <%= @book.book_comments.count %></p></td> <td id="star-rate-<%= @book.id %>"></td> <script> $('#star-rate-<%= @book.id %>').raty({ size: 36, starOff: '<%= asset_path('star-off.png') %>', starOn: '<%= asset_path('star-on.png') %>', starHalf: '<%= asset_path('star-half.png') %>', half: true, readOnly: true, score: <%= @book.rate %>, }); </script> <% if @book.user == current_user %> <td><%= link_to 'Edit', edit_book_path(@book), class: "btn btn-sm btn-success edit_book_#{@book.id}" %></td> <td><%= link_to 'Destroy', book_path(@book), method: :delete, data: { confirm: '本当に消しますか?' }, class: "btn btn-sm btn-danger destroy_book_#{@book.id}"%></td> <% end %> </tr> </table> <table class="book_comments"> <%= render "book_comments/index",book: @book %> </table> <%= form_with model:[@book,@book_comment], remote:true do |f| %> <%= f.text_area :comment,size: "90x5" %><br> <%= f.submit "送信" %> <% end %> </div> </div> </div>
試したこと
pry-byebugを使用し投稿後の@bookを確認してみたところ、rateカラムがnilで保存されていないことがわかりました。
しかしテーブル内ではfloat型として定義しており、
投稿フォーム内のscriptタグ内ではscoreName: 'book[rate]',と記述しているので、どこに問題があるかわからないです。
ご教授のほど、よろしくお願いいたします。
補足情報(FW/ツールのバージョンなど)
こちらの記事を実装時に参考させていただきました。
参考記事
まだ回答がついていません
会員登録して回答してみよう