J Seabolt
J Seabolt

Reputation: 2978

ActiveRecord Rollback when trying to save to database

I am building a fairly simple album review application. I am attempting to set up a simple_form for new reviews. The change will not stick. This is what appears in my terminal when I attempt to add a review:

Started POST "/albums/6/reviews" for ::1 at 2017-01-10 11:36:30 -0800
Processing by ReviewsController#create as HTML
Parameters: {"utf8"=>"✓",     "authenticity_token"=>"Ld5yOdf4fbzQvyPdckfAtnjWqbDQQHkEZuszSrdlsjVfRYntGZymxwP80DI xe4O8Pe5X7cl7P2ioCR1bQ//waw==", "review"=>{"comment"=>"I like DSOTM"},   "commit"=>"Create Review", "album_id"=>"6"}
Album Load (0.4ms)  SELECT  "albums".* FROM "albums" WHERE "albums"."id" = ? LIMIT ?  [["id", 6], ["LIMIT", 1]]
(0.2ms)  begin transaction
(0.2ms)  rollback transaction
Rendering reviews/new.html.erb within layouts/application
Rendered reviews/_form.html.erb (6.8ms)
Rendered reviews/new.html.erb within layouts/application (17.8ms)
Completed 200 OK in 251ms (Views: 222.5ms | ActiveRecord: 0.7ms)

Not sure what's going on. Any help would be appreciated. Here are my routes:

Rails.application.routes.draw do
  devise_for :users
  resources :albums do
    resources :reviews
  end
  root 'albums#index'
end

Here is the review form I am attempting to build:

<%= simple_form_for([@album, @album.reviews.build]) do |f| %>
   <%= f.error_notification %>
   <div class="form-inputs">
    <%= f.input :comment %>
   </div>
   <div class="form-actions">
    <%= f.button :submit %>
   </div>
<% end %>

Here is the reviews controller(have not finished this. Basically an edited scaffold):

class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy]
before_action :set_album

def index
@reviews = Review.all
end

def show
end

def new
@review = Review.new
end

def edit
end

def create
@review = Review.new(review_params)

respond_to do |format|
  if @review.save
    format.html { redirect_to @review, notice: 'Review was successfully created.' }
    format.json { render :show, status: :created, location: @review }
  else
    format.html { render :new }
    format.json { render json: @review.errors, status: :unprocessable_entity }
  end
end
end


def update
respond_to do |format|
  if @review.update(review_params)
    format.html { redirect_to @review, notice: 'Review was successfully updated.' }
    format.json { render :show, status: :ok, location: @review }
  else
    format.html { render :edit }
    format.json { render json: @review.errors, status: :unprocessable_entity }
  end
end
end

def destroy
@review.destroy
respond_to do |format|
  format.html { redirect_to reviews_url, notice: 'Review was successfully destroyed.' }
  format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_review
  @review = Review.find(params[:id])
end
def set_album
  @album = Album.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
  params.require(:review).permit(:comment)
end
end

Review model:

class Review < ApplicationRecord
belongs_to :user 
belongs_to :album
end

Album model:

class Album < ApplicationRecord
has_many :reviews
belongs_to :user
end

Upvotes: 0

Views: 257

Answers (1)

coreyward
coreyward

Reputation: 80041

A validation error is preventing the model from being persisted. You're not seeing that error because you create a new instance of it in this line:

simple_form_for [@album, @album.reviews.build]

You ought to build the review in advance in your controller, and replace that line with:

simple_form_for [@album, @review]

Then you should see the validation error. If this still isn't showing, or if one of your attributes gets erased, it might be because your parameters whitelist is excluding them. You can get more info by building and validating the object in the Rails Console, then checking the errors on the model instance.

Upvotes: 1

Related Questions