【问题标题】:How can I use variables within model?如何在模型中使用变量?
【发布时间】:2013-01-03 16:40:14
【问题描述】:

我正在尝试将页面的 URL 插入到上传的图片中。
我已经有下面这样的代码,但它不起作用。

我的模型有问题吗?我该如何解决这个问题?

我的关联

  • 用户 has_one :profile
  • 个人资料属于_to:用户

models/user.rb

before_save :text_to_insert? 

def text_to_insert
    nickname = self.profile.nickname 
end

has_attached_file :user_avatar,
    :styles => {
    :thumb=> "100x100>",
    :small  => "400x400>" }, 
     :convert_options => {
     :small => '-fill white  -undercolor "#00000080"  -gravity South -annotate +0+5 " example.com/'+ nickname +' "' } 

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 model


【解决方案1】:

在保存之前,您正在使用 text_to_insert? 方法,该方法不存在,因此返回 false,因此无法保存。 好像有错别字,尝试在:text_to_insert 之后删除?

before_save :text_to_insert

请确保self.profile.nickname是有效的

【讨论】:

  • 还是说这个错误syntax error, unexpected tUPLUS, expecting keyword_do or '{' or '(' :convert_options的编码方式不对吗?
  • 如果我编码self.profile.nickname,这里的self表示用户的当前访问记录对吗?
  • 尝试检查has_attached_file并检查它是否接受options={}。通常这是p = { :n => 'd' } 有效代码,:n => {:s => 'bar'} 无效
猜你喜欢
  • 1970-01-01
  • 2018-02-05
  • 2017-04-23
  • 1970-01-01
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
相关资源
最近更新 更多