我的大脑越来越纠结。我有用户,他们可以有一个计划和一个订阅。我正在使用ryan bates示例进行订阅,除了我不知道如何将user_id纳入订阅之外,其他一切都在工作
这是我的订阅控制器。
class SubscriptionsController < ApplicationController
before_action :authenticate_user!
def new
plan = Plan.find(params[:plan_id])
@subscription = plan.subscriptions.build
end
def create
@subscription = Subscription.new(subscription_params)
if @subscription.save
redirect_to @subscription, :notice => "Thank you for subscribing!"
else
render :new
end
end
def show
@subscription = Subscription.find(params[:id])
end
private
def subscription_params
params.require(:subscription).permit(:plan_id, :email, :user_id)
end
end
这是我的用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :videos
has_many :votes
has_many :favorites
has_many :videos_with_votes, :through => :votes, :source => :video
has_many :videos_with_favorites, :through => :favorites, :source => :video
has_one :subscription
has_one :plan, :through => :subscription
has_attached_file :avatar, :styles => { :medium => "300x300#", :thumb => "80x80#" }
def voted?(video)
votes.exists?(video_id: video.id)
end
def favorited?(video)
favorites.exists?(video_id: video.id)
end
end
这是我的计划模型
class Plan < ActiveRecord::Base
has_many :subscriptions
has_many :users
end
这是我的订阅模式
class Subscription < ActiveRecord::Base
belongs_to :plan
belongs_to :user
validates_presence_of :plan_id
validates_presence_of :email
end