如何让我的链接命中正确的控制器操作?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
@topicable
是一个Video
对象,那么它将转到VideosController
。您需要手动设置路径:If
@topicable
is of aVideo
object then it will go to theVideosController
. You need to set the path manually: