好的,我有点新鲜;但我真的不知道该怎么做:
我有资产模型,文章模型和用户模型。
资产模型具有回形针文档属性。
#asset.rb
belongs_to :article
has_attached_file :document,
:path => "assets/:id-:basename-:style.:extension",
storage: :dropbox,
dropbox_credentials: Rails.root.join("config/dropbox.yml")
validates_attachment :document, content_type: { content_type: "application/pdf" }
#article.rb
belongs_to :user
has_many :assets, dependent: :destroy
accepts_nested_attributes_for :assets, allow_destroy: true, :reject_if => lambda { |d| d[:document].blank? }
validates :title, :content, presence: true
#articles_controller.rb
def new
@article = Article.new
@article.assets.build
end
def article_params
params.require(:article).permit(:title, :content,
assets_attributes: [:id, :article_id, :document, :_destroy]
)
end
#user.rb
has_many :articles, dependent: :destroy
has_many :assets, through: :articles
REGEX_EMAIL = /\A([\w+\-.]+)(@[\w+\-.]+)(\.[a-z]+)\z/i
validates :email, format: { with: REGEX_EMAIL }, uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
has_secure_password
validates :dp_key, :dp_secret, presence: true
#config/dropbox.yml
app_key: "r3g00ca3v1hfwjj"
app_secret: "dkixp9rgqkkyflma"
access_token: "13dn69hab311ij35"
access_token_secret: "hq1jniuyjpa791y"
user_id: "14864638"
access_type: "app_folder"
每个用户都有自己的Dropbox apikey属性。我想要做的是每当他们上传文档资产时,如果用户没有保存的drop apikey,我希望资产模型使用用户的API密钥而不是默认密钥。
我假设我需要将一些变量从控制器传递给模型?
非常感谢任何帮助,谢谢!