0

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

enter image description here

1
  • I created the data in rails console, and I've confirmed that the db also reflects the rate, and I'm even able to output it.
    – jimbo5922
    Apr 25, 2020 at 5:34

1 Answer 1

0

It was resolved.I added :rate in "def micropost_params" of microposts_controller.rb as follows, and it works.

class MicropostsController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]
  before_action :correct_user, only: :destroy

  def create
    @micropost = current_user.microposts.build(micropost_params)
    if @micropost.save
      flash[:success] = "投稿しました"
      redirect_to root_url
    else
      @feed_items = []
      render 'new'
    end
  end

  def destroy
    @micropost.destroy
    flash[:success] = "投稿を削除しました"
    redirect_to request.referrer || root_url
  end

  def new
    @micropost = current_user.microposts.build if logged_in?
  end

  def show 
    @micropost = Micropost.find(params[:id])
    @comment = Comment.new
    @user = User.find_by(id: @micropost.user_id)
  end

  private

    def micropost_params
      params.require(:micropost).permit(:content, :picture, :rate)
    end

    def correct_user
      @micropost = current_user.microposts.find_by(id: params[:id])
      redirect_to root_url if @micropost.nil?
    end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.