我开发了一个应用程序,我的协会似乎遇到了一些问题。我有以下内容:
class User < ActiveRecord::Base
acts_as_authentic
has_many :questions, :dependent => :destroy
has_many :sites , :dependent => :destroy
end
问题
class Question < ActiveRecord::Base
has_many :sites, :dependent => :destroy
has_many :notes, :through => :sites
belongs_to :user
end
网站(将其视为问题的答案)
class Site < ActiveRecord::Base
acts_as_voteable :vote_counter => true
belongs_to :question
belongs_to :user
has_many :notes, :dependent => :destroy
has_many :likes, :dependent => :destroy
has_attached_file :photo, :styles => { :small => "250x250>" }
validates_presence_of :name, :description
end
创建站点(答案)时,我成功将question_id传递给Sites表,但我无法弄清楚如何传递user_id。这是我的SitesController #create
def create
@question = Question.find(params[:question_id])
@site = @question.sites.create!(params[:site])
respond_to do |format|
format.html { redirect_to(@question) }
format.js
end
end
答案 0 :(得分:0)
我认为这可以胜任
@question = current_user.questions.find params[:question_id]
如果没有,那么只需指定mannualy。
@site = @question.sites.build(params[:site])
@site.user = current_user
@site.save