在我的Rails项目中,我正在创建一个带有关联的教程 视频。当我尝试提交此表单时,在我的rails控制台中,我不断收到如下所示的ROLLBACK。谁能向我解释为什么它不能保存到数据库?
我的表单:
<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 %>
我的控制器:
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
我的教程模型:
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
我的视频模型:
class Video < ApplicationRecord
has_many :user_videos
has_many :users, through: :user_videos
belongs_to :tutorial
validates_presence_of :position
end
我的Rails控制台日志:
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)
答案 0 :(得分:1)
在您的TutorialsController中,您可以使用为您创建的帮助程序方法来构建视频。
将第5行更改为
@tutorial.videos.build
到
@tutorial.build_videos