In my Rails project, I am creating a Tutorial with an associated Video. When I try to submit this form, in my rails console I keep getting a ROLLBACK as shown below. Can anyone explain to my why it won't save to the database?
My form:
<h2>New Tutorial</h2>
<%= form_for [:admin, @tutorial] do |f| %>
<%= f.label :title %>
<%= f.text_field :title, class: "block col-4 field" %>
<%= f.label :description %>
<%= f.text_area :description, class: "block col-4 field" %>
<%= f.label :thumbnail %>
<%= f.text_field :thumbnail, class: "block col-4 field" %>
<h3>Videos</h3>
<%= f.fields_for :videos do |builder| %>
<%= builder.label :title %>
<%= builder.text_field :title, class: "block col-4 field" %>
<%= builder.label :description %>
<%= builder.text_field :description, class: "block col-4 field" %>
<%= builder.label :thumbnail %>
<%= builder.text_field :thumbnail, class: "block col-4 field" %>
<%= builder.label :video_id %>
<%= builder.text_field :video_id, class: "block col-4 field" %>
<%= builder.label :position %>
<%= builder.text_field :position, class: "block col-4 field" %>
<% end %>
<%= f.submit "Save", class: "mt2 btn btn-primary mb1 bg-teal" %>
<% end %>
My controller:
class Admin::TutorialsController < Admin::BaseController
def new
@tutorial = Tutorial.new
@tutorial.videos.build
end
def create
@tutorial = Tutorial.new(tutorial_params)
if @tutorial.save
flash[:success] = "Successfully created tutorial!"
redirect_to tutorial_path(@tutorial)
else
render :new
end
end
def edit
@tutorial = Tutorial.find(params[:id])
end
def update
tutorial = Tutorial.find(params[:id])
if tutorial.update(tutorial_params)
flash[:success] = "#{tutorial.title} tagged!"
end
redirect_to edit_admin_tutorial_path(tutorial)
end
private
def tutorial_params
params.require(:tutorial).permit(:title, :description, :thumbnail, videos_attributes: [:title, :description, :video_id, :thumbnail, :position])
end
end
My tutorial model:
class Tutorial < ApplicationRecord
has_many :videos, -> { order(position: :ASC) }, dependent: :destroy
acts_as_taggable_on :tags, :tag_list
accepts_nested_attributes_for :videos
scope :without_classroom, -> { where(classroom: false) }
end
And my video model:
class Video < ApplicationRecord
has_many :user_videos
has_many :users, through: :user_videos
belongs_to :tutorial
validates_presence_of :position
end
And my rails console log:
Started POST "/admin/tutorials" for 127.0.0.1 at 2019-02-07 10:11:18 -0700
Processing by Admin::TutorialsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mVD5oCSIAtsTZ86ksWFw4DEpSu2YS89333PK19fhGuck7HMuyoNs3eCulOX2SpX/IPvcPdywMxSxNoa9Q0cu9g==", "tutorial"=>{"title"=>"Sweet nothing", "description"=>"I am here", "thumbnail"=>"www.google.com", "videos_attributes"=>{"0"=>{"title"=>"This thing", "description"=>"Here we are", "thumbnail"=>"kldsfjdsk", "video_id"=>"6", "position"=>"7"}}}, "commit"=>"Save"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:8
(0.2ms) BEGIN
↳ app/controllers/admin/tutorials_controller.rb:10
(0.2ms) ROLLBACK
↳ app/controllers/admin/tutorials_controller.rb:10
Rendering admin/tutorials/new.html.erb within layouts/application
Rendered admin/tutorials/new.html.erb within layouts/application (4.1ms)
Rendered partials/_nav.html.erb (1.9ms)
Completed 200 OK in 93ms (Views: 28.7ms | ActiveRecord: 13.7ms)
Aucun commentaire:
Enregistrer un commentaire