Implemented star rating function using raty on rails6.
First, put Jquery https://qiita.com/masahisa/items/eaacb0c3b82f4a11fc13
Please introduce jquery referring to this. Become rails6 It is easy to forget the description of $ yarn add jquery, so be careful.
Save star image from github https://github.com/wbotelhos/raty/tree/master/lib/images Save this in app / assets / images.
in app / assets / config / manifest.js
in app / layouts / application.html.erb
This is the environment setting. Please note that the method is very different except for rails6. (I'm not sure if this method is correct even in 6, but I was able to read it with this.)
Implementation of the following functions.
Added rate column
rails g migration AddRateToEvaluations rate:float ↓ rails db:migrate
In the form of the view you want to add a star rating function
<div class="form-group row" id="star">
<% = f.label: rate,'Comprehensive evaluation', class:'col-md-3 col-form- label' %> <%= f.hidden_field :rate, id: :review_star %>
Describe. The point is to define review_star for id in hidden_field. Load the star image saved in the Js part.
Don't forget to permit the rate parameter in your controller. You should now be ready to type with a star.
For the time being, I will also include the code that displays the entered data. In my case, there is a comment column in the evaluations table, and the post model and user model are associated with each other, so please use it as a reference only.
<tbody> <% @evaluations.each do |evaluation| %> <tr> <td><%= evaluation.comment %></td> <td> <a href="/users/<%= evaluation.user.id %>"> <%= evaluation.user.nickname %> </a></td>
<% end %>
<!-/ Star rating->
This is the image. There is no article on rails6, and I made it by force with reference to articles other than 6, so I think the correct answer is different.