Ruby on rails 如何从控制器操作中调用另一个控制器操作?
因此我有一个Ruby on rails 如何从控制器操作中调用另一个控制器操作?,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,因此我有一个节点,它可以是不同的类型(一个注释&视频) 我想做的是,每当我删除节点,它也应该删除它指向的底层对象-例如视频 以下是我的模型(为简洁起见,必要时将其截断): Node.rb # == Schema Information # # Table name: nodes # # id :integer not null, primary key # name :string(255) # family_tree_id :
节点
,它可以是不同的类型(一个注释
&视频
)
我想做的是,每当我删除节点
,它也应该删除它指向的底层对象-例如视频
以下是我的模型(为简洁起见,必要时将其截断):
Node.rb
# == Schema Information
#
# Table name: nodes
#
# id :integer not null, primary key
# name :string(255)
# family_tree_id :integer
# user_id :integer
# media_id :integer
# media_type :string(255)
# created_at :datetime
# updated_at :datetime
# circa :datetime
# is_comment :boolean
#
class Node < ActiveRecord::Base
belongs_to :family_tree
belongs_to :user
belongs_to :media, polymorphic: true
has_many :comments, dependent: :destroy
has_many :node_comments, dependent: :destroy
def is_video?
self.media_type == 'Video'
end
def comment_count
self.node_comments.count + self.comments.count
end
end
# == Schema Information
#
# Table name: comments
#
# id :integer not null, primary key
# user_id :integer
# message :text
# node_id :integer
# created_at :datetime
# updated_at :datetime
#
class Comment < ActiveRecord::Base
validates :message, presence: true
belongs_to :user
belongs_to :node
def self.search(query)
where("title like ? OR description like ?", "%#{query}%", "%#{query}%")
end
end
Comment.rb
# == Schema Information
#
# Table name: nodes
#
# id :integer not null, primary key
# name :string(255)
# family_tree_id :integer
# user_id :integer
# media_id :integer
# media_type :string(255)
# created_at :datetime
# updated_at :datetime
# circa :datetime
# is_comment :boolean
#
class Node < ActiveRecord::Base
belongs_to :family_tree
belongs_to :user
belongs_to :media, polymorphic: true
has_many :comments, dependent: :destroy
has_many :node_comments, dependent: :destroy
def is_video?
self.media_type == 'Video'
end
def comment_count
self.node_comments.count + self.comments.count
end
end
# == Schema Information
#
# Table name: comments
#
# id :integer not null, primary key
# user_id :integer
# message :text
# node_id :integer
# created_at :datetime
# updated_at :datetime
#
class Comment < ActiveRecord::Base
validates :message, presence: true
belongs_to :user
belongs_to :node
def self.search(query)
where("title like ? OR description like ?", "%#{query}%", "%#{query}%")
end
end
VideoController#销毁
def destroy
@node.destroy
respond_to do |format|
format.html { redirect_to root_path }
format.json { head :no_content }
end
end
def destroy
authorize! :read, @family_tree
@video = Video.find(params[:id])
if Video.delete_video(@video)
flash[:notice] = "video successfully deleted"
else
flash[:error] = "video unsuccessfully deleted"
end
redirect_to dashboard_index_path
end
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to comments_url }
format.json { head :no_content }
end
end
CommentController#销毁
def destroy
@node.destroy
respond_to do |format|
format.html { redirect_to root_path }
format.json { head :no_content }
end
end
def destroy
authorize! :read, @family_tree
@video = Video.find(params[:id])
if Video.delete_video(@video)
flash[:notice] = "video successfully deleted"
else
flash[:error] = "video unsuccessfully deleted"
end
redirect_to dashboard_index_path
end
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to comments_url }
format.json { head :no_content }
end
end
所以我想做的就是,每当有一个节点被删除时,我想让它删除相关的视频和评论
我尝试将其添加到我的节点.rb
模型中,但当我点击delete时,它返回了一个“堆栈级别太深错误”:
我想你想把
dependent::destroy
从
在video.rb中有一个节点:node,as::media,:dependent=>:destroy。那么,在node.rb中使用这一行时,不应该得到无限递归:
属于:媒体,多态:真实,依赖::销毁
Hrmm…有趣。我仍然想弄清楚如何删除相关的对象。想法?我很高兴这有帮助。
def destroy
@node.destroy
respond_to do |format|
format.html { redirect_to root_path }
format.json { head :no_content }
end
end
def destroy
authorize! :read, @family_tree
@video = Video.find(params[:id])
if Video.delete_video(@video)
flash[:notice] = "video successfully deleted"
else
flash[:error] = "video unsuccessfully deleted"
end
redirect_to dashboard_index_path
end
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to comments_url }
format.json { head :no_content }
end
end