如何让我的链接命中正确的控制器操作?

发布于 2024-10-29 11:19:37 字数 2272 浏览 4 评论 0原文

我有一个 has_many,通过视频和主题之间的关联,以主题作为独立资源。我想要一个链接,删除特定的主题记录(又名关联),而不是视频或主题。

我的主题控制器中有此方法:

def destroy
  @topicable = Topicable.find(params[:id])
  @topicable.destroy
  respond_to do |format|
    format.html {redirect_to @video}
    format.js
  end
end

我想使用此链接调用上述方法我的视频显示视图:

<%= link_to "x", @topicable, :method => :delete, :class => 'topic_delete' %>

但是,这并没有达到我想要的效果。相反,它似乎点击了视频控制器删除操作并删除了视频和关联,而不仅仅是关联,这就是我想要的。这些是日志:

Started POST "/videos/468" for 127.0.0.1 at Fri Apr 01 19:15:07 -0700 2011
  Processing by VideosController#destroy as HTML
  Parameters: {"authenticity_token"=>"F2uF4FDDSPw+jHHGtsosGLjHwkDUg/nre5u+WEPPDUY=", "id"=>"468"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 57 LIMIT 1
  Video Load (0.3ms)  SELECT "videos".* FROM "videos" WHERE "videos"."id" = 468 AND ("videos".user_id = 57) ORDER BY videos.rank_sum DESC LIMIT 1
  Genre Load (0.3ms)  SELECT * FROM "genres" INNER JOIN "genres_videos" ON "genres".id = "genres_videos".genre_id WHERE ("genres_videos".video_id = 468 )
  Topicable Load (0.3ms)  SELECT "topicables".* FROM "topicables" WHERE ("topicables".video_id = 468)
  AREL (0.4ms)  DELETE FROM "topicables" WHERE "topicables"."id" = 93
  AREL (0.1ms)  DELETE FROM "topicables" WHERE "topicables"."id" = 94
  AREL (0.1ms)  DELETE FROM "videos" WHERE "videos"."id" = 468
Redirected to http://localhost:3000/videos
Completed 302 Found in 181ms

如何解决此问题?

这是视频模型:

validates :video_url, :presence => true
validates :title, :presence => true

belongs_to :user 
has_many :video_votes
has_many :voted_users, :through => :video_votes, :source => :user
has_and_belongs_to_many :genres
has_many :topicables, :dependent => :destroy
has_many :topics, :through => :topicables

attr_accessor :topic_names
after_save :assign_topics
before_update :update_rank_sum
default_scope order('videos.rank_sum DESC')

def assign_topics
  if @topic_names
    self.topics << @topic_names.map do |name|
      Topic.find_or_create_by_name(name)
    end
  end
end

可主题模型:

belongs_to :video
belongs_to :topic

I have a has_many, through association between videos and topics with topicables as the independent resource. I want to have a link that deletes that specific topicable record aka the association and NOT the video NOR the topic.

I have this method in my topicables controller:

def destroy
  @topicable = Topicable.find(params[:id])
  @topicable.destroy
  respond_to do |format|
    format.html {redirect_to @video}
    format.js
  end
end

I want to call the above method with this link in my video show view:

<%= link_to "x", @topicable, :method => :delete, :class => 'topic_delete' %>

However, this does not do what I want it to do. Instead, it seems to hit the videos controller delete action and delete both the video AND the associations, rather than just the associations, which is what I want. These are the logs:

Started POST "/videos/468" for 127.0.0.1 at Fri Apr 01 19:15:07 -0700 2011
  Processing by VideosController#destroy as HTML
  Parameters: {"authenticity_token"=>"F2uF4FDDSPw+jHHGtsosGLjHwkDUg/nre5u+WEPPDUY=", "id"=>"468"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 57 LIMIT 1
  Video Load (0.3ms)  SELECT "videos".* FROM "videos" WHERE "videos"."id" = 468 AND ("videos".user_id = 57) ORDER BY videos.rank_sum DESC LIMIT 1
  Genre Load (0.3ms)  SELECT * FROM "genres" INNER JOIN "genres_videos" ON "genres".id = "genres_videos".genre_id WHERE ("genres_videos".video_id = 468 )
  Topicable Load (0.3ms)  SELECT "topicables".* FROM "topicables" WHERE ("topicables".video_id = 468)
  AREL (0.4ms)  DELETE FROM "topicables" WHERE "topicables"."id" = 93
  AREL (0.1ms)  DELETE FROM "topicables" WHERE "topicables"."id" = 94
  AREL (0.1ms)  DELETE FROM "videos" WHERE "videos"."id" = 468
Redirected to http://localhost:3000/videos
Completed 302 Found in 181ms

How do I fix this problem?

Here's the video model:

validates :video_url, :presence => true
validates :title, :presence => true

belongs_to :user 
has_many :video_votes
has_many :voted_users, :through => :video_votes, :source => :user
has_and_belongs_to_many :genres
has_many :topicables, :dependent => :destroy
has_many :topics, :through => :topicables

attr_accessor :topic_names
after_save :assign_topics
before_update :update_rank_sum
default_scope order('videos.rank_sum DESC')

def assign_topics
  if @topic_names
    self.topics << @topic_names.map do |name|
      Topic.find_or_create_by_name(name)
    end
  end
end

Topicable model:

belongs_to :video
belongs_to :topic

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沐歌 2024-11-05 11:19:37

如果@topicable是一个Video对象,那么它将转到VideosController。您需要手动设置路径:

<%= link_to "x", topicable_path(@topicable), ... %>

If @topicable is of a Video object then it will go to the VideosController. You need to set the path manually:

<%= link_to "x", topicable_path(@topicable), ... %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文