I'm now trying to add a rating feature to my app. I added a rate column to the DB for that purpose. However, a problem has arisen. The columns have been added to the DB, but I don't seem to be able to save them to the DB from the micropost submission form. I checked to see if there was anything wrong with the code I wrote, but I can't think of any cause. I would appreciate your advice.
new.html.erb
<% provide(:title, '新規投稿') %>
<h1>新規投稿</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_with(model: @micropost, local: true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "本文を入力" %>
</div>
<div class="micropost_rate">
<p>海水浴場の評価</p>
<%= f.radio_button :rate, :"1" %>
<%= f.label :rate, 1, value: :"1" %>
<%= f.radio_button :rate, :"2" %>
<%= f.label :rate, 2, value: :"2" %>
<%= f.radio_button :rate, :"3" %>
<%= f.label :rate, 3, value: :"3" %>
<%= f.radio_button :rate, :"4" %>
<%= f.label :rate, 4, value: :"4" %>
<%= f.radio_button :rate, :"5" %>
<%= f.label :rate, 5, value: :"5" %>
</div>
<%= f.submit "投稿する", class: "btn btn-primary" %>
<span class="picture">
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
</span>
<% end %>
</div>
</div>
<script type="text/javascript">
$('#micropost_picture').bind('change', function() {
var size_in_megabytes = this.files[0].size/1024/1024;
if (size_in_megabytes > 5) {
alert('Maximum file size is 5MB. Please choose a smaller file.');
}
});
</script>
_micropost.html.erb
<li id="micropost-<%= micropost.id %>" data-micropost-id="<%= micropost.id %>">
<%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
<%= link_to micropost.user.name, micropost.user %>
<span class="content">
<%= micropost.content %>
<%= image_tag micropost.picture.url if micropost.picture? %>
</span>
<span class="timestamp">
<%= time_ago_in_words(micropost.created_at) %>前に投稿
<%= link_to "コメントする", micropost %>
<% if current_user?(micropost.user) %>
<%= link_to "削除", micropost, method: :delete, data: { confirm: "削除してもよろしいですか?" } %>
<% end %>
</span>
<%= render "likes/like", micropost: micropost %>
<% if logged_in? %>
<% if micropost.favorited_by?(current_user) %>
<p>
<span>お気に入り解除: </span>
<%=link_to micropost.favorites.count, micropost_favorites_path(micropost.id), method: :delete %>
</p>
<% else %>
<p>
<span>お気に入り: </span>
<%=link_to micropost.favorites.count, micropost_favorites_path(micropost.id), method: :post %>
</p>
<% end %>
<% else %>
<p>
<span>お気に入り数: </span>
<%= micropost.favorites.count %>
</p>
<% end %>
<span>
<p>海水浴場の評価</p>
<%= @micropost.rate %>
</span>
</li>
schema.rb
ActiveRecord::Schema.define(version: 2020_04_24_043531) do
.
.
.
create_table "microposts", force: :cascade do |t|
t.text "content"
t.integer "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "picture"
t.integer "likes_count", default: 0, null: false
t.string "rate"
t.index ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at"
t.index ["user_id"], name: "index_microposts_on_user_id"
end
.
.
.
add_foreign_key "comments", "microposts"
add_foreign_key "comments", "users"
add_foreign_key "microposts", "users"
end