0

大家好,模型中的 Player.rb 文件中的每个人我都设置了这个.. 工作得很好。

before_create :generate_random_hex

  private
    def generate_random_hex
  rand(9999)
    end

    Paperclip.interpolates :random_hex do |attachment, style|
    attachment.instance.random_hex
    end

    has_attached_file :avatar, :styles => { :profile => "300x300", :thumb => "100x100#"},
    :url  => "/assets/people/:id/:style/:basename.:extension",
    :path => ":rails_root/public/assets/people/:id/:style/:basename_:random_hex.:extension"

    validates_attachment_size         :avatar, :less_than    => 2.megabytes # Solo aceptar imágenes menores a 2 Mb.
    validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif']

问题是我有其他控制器可以“更新”玩家的个人资料。

我收到此错误“未定义的方法 `random_hex' for #”

这是我如何尝试调用我认为需要使用 Player.generate_random_hex 的方法?

或者我做错了什么?

include Wicked::Wizard
  steps :personal_information, :experience, :skills, :references_achievements, :personal_profile, :paypal

  def show
    @player = current_user.player
    @player.clubs.build
    @player.links.build
    @player.references.build
    @player.achievements.build
    @player.citizens.build
    render_wizard

    authorize! :show, :player_steps
  end

  def update

    case step
      when :personal_information
        params[:player][:language_ids] ||= []
      when :experience
        params[:player][:selection_ids] ||= []
    end

    @player = current_user.player
    @player.update_attributes(params[:player])
    render_wizard @player

    authorize! :update, :player_steps
  end
4

1 回答 1

0

尝试这个:

Paperclip.interpolates :random_hex do |attachment, style|
  attachment.instance.generate_random_hex
end

# without `private`
def generate_random_hex
  rand(9999)
end
于 2013-09-18T05:50:28.437 回答