【问题标题】:undefined method Rails未定义的方法 Rails
【发布时间】:2013-09-18 02:14:34
【问题描述】:

大家好,我在模型中的 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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 devise paperclip


    【解决方案1】:

    试试这个:

    Paperclip.interpolates :random_hex do |attachment, style|
      attachment.instance.generate_random_hex
    end
    
    # without `private`
    def generate_random_hex
      rand(9999)
    end
    

    【讨论】:

      猜你喜欢
      • 2014-04-04
      • 2013-02-05
      • 1970-01-01
      • 2013-08-30
      • 2015-10-03
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多