Simple_form not saving rating value. Keeps showing 'nil'


#1

Not sure what to do to fix it…

Html:

<h2>Post your Review For <%= @restaurant.name %></h2>
<%= simple_form_for [@restaurant, @review] do |f| %>
  <%= f.input :content %>
  <%= f.input :rating, input_html: { type: 'integer' } %>
  <%= f.submit "Post Review", class: "btn btn-primary" %>
<% end %>

Controller:

def create
    @review = Review.new(review_params)
    # we need `restaurant_id` to associate review with corresponding restaurant
    @restaurant = Restaurant.find(params[:restaurant_id])
    @review.restaurant = @restaurant
    @review.save
    raise
    redirect_to restaurant_path(@restaurant)
  end

schema:

create_table "reviews", force: :cascade do |t|
    t.text "content"
    t.integer "rating"
    t.integer "restaurant_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["restaurant_id"], name: "index_reviews_on_restaurant_id"
  end

Result:

@review
=> #<Review id: 6, content: “bro”, rating: nil, restaurant_id: 2, created_at: “2020-01-04 21:17:07”, updated_at: “2020-01-04 21:17:07”>


#2

@review.errors
=> #<ActiveModel::Errors:0x00007f8c89812640 @base=#<Review id: 7, content: “ggg”, rating: nil, restaurant_id: 1, created_at: “2020-01-04 22:34:15”, updated_at: “2020-01-04 22:34:15”>, @messages={}, @details={}>

@review.errors.full_message
ArgumentError: wrong number of arguments (given 0, expected 2)
from /Users/Prodigy/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.3/lib/active_model/errors.rb:367:in full_message' from /Users/Prodigy/code/PierceF/rails-yelp-mvp/app/controllers/reviews_controller.rb:14:increate’


#3

FIXED:
review_params (aka strong params) in reviews controller. Its purpose is exactly to filter out anything you don’t permit. So if there’s no mention of :rating there it will and should be filtered out. So I added in :rating