Rails has_and_belongs_to_many关联

|| 所以我有两个模型:
class User < ActiveRecord::Base
  has_and_belongs_to_many :followed_courses,  :class_name => \"Course\"
end

class Course < ActiveRecord::Base
  has_and_belongs_to_many :followers, :class_name => \"User\"
end
在User.rb中,我还有:
  def following_course?(course)
    followed_courses.include?(course)
  end

  def follow_course!(course)
    followed_courses<<course
  end

  def unfollow_course!(course)
    followed_courses.delete(course)
  end
我没有courses_users模型,只有一个联接表(courses_users)。我想我必须遵循/取消遵循CoursesController中的课程。是否在控制器中创建新动作? 现在,当我不遵循课程时,我在课程/显示页面上有一个关注表格
= form_for @course, :url => { :action => \"follow\" }, :remote => true do |f|
  %div= f.hidden_field :id
  .actions= f.submit \"Follow\"
我在CoursesController中有:
  def follow
    @course = Course.find(params[:id])
    current_user.follow_course!(@course)
    respond_to do |format|
      format.html { redirect_to @course }
      format.js
    end
  end
但是看起来它从未被激活过。我是否需要修改路线以便激活操作?怎么修改呢?还是有更好的方法来做到这一点?我可以仅用链接替换表格吗?提前致谢! 这是相关问题Rails多态模型实现的后续活动     
已邀请:
路由系统调用“ 4”?如果不是,则必须在
routes.rb
文件中写以下行:
map.resources :courses, :member => {:follow => :post} 
#You\'ll have the map.resources :courses, just add the second argument.
之后,路由系统可以重定向到该操作,然后您将提供“ 7”帮助程序     

要回复问题请先登录注册