Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails模型直接使用其他类方法_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails Rails模型直接使用其他类方法

Ruby on rails Rails模型直接使用其他类方法,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在做Rails应用程序,我发现我可以重构我的代码。只是找不到方法我有很多模型,如公司、新闻、个人资料等,都使用图像上传。因此,在每个类中,我必须始终复制粘贴30行方法,这些方法始终实现相同的逻辑-上载图像、获取图像名称、删除图像我的模型类将自动拥有其他地方的方法,这怎么可能呢?我只想把模型加载到“GlobalMethods”中,或者甚至在activerecord:base中包含methods,以便在每个类中都使用它们,并且我可以在任何时候使用它们

我正在做Rails应用程序,我发现我可以重构我的代码。只是找不到方法
我有很多模型,如公司、新闻、个人资料等,都使用图像上传。因此,在每个类中,我必须始终复制粘贴30行方法,这些方法始终实现相同的逻辑-上载图像、获取图像名称、删除图像
我的模型类将自动拥有其他地方的方法,这怎么可能呢?我只想把模型加载到“GlobalMethods”中,或者甚至在activerecord:base中包含methods,以便在每个类中都使用它们,并且我可以在任何时候使用它们<从控制器那里,我就这样离开了。例如-News.upload_image,它的功能与原始模型中的逻辑相同
请举例说明,因为我读了更多的书,不理解,甚至不可能。

此时此刻,我做到了:

和型号:

class Company < ActiveRecord::Base
    include Uploading
end
class公司
我得到了这个错误:

未初始化的常量公司::正在上载

我的rails版本:“4.2.5”
每次尝试后,我都会重新启动服务器
My application.rb文件看起来:

require File.expand_path('../boot', __FILE__)

require 'rails/all'

Bundler.require(*Rails.groups)

module Vca
  class Application < Rails::Application
    config.active_record.raise_in_transactional_callbacks = true
    config.autoload_paths += %W(
      #{config.root}/app/models/concerns/uploading.rb
    )
  end
end
需要文件。展开路径('../boot',文件)
需要“rails/all”
Bundler.require(*Rails.groups)
模块Vca
类应用程序
还是一样

CompanyController#索引中的名称错误

未初始化的常量公司::正在上载

class公司
比如说

app/models/concerns/my_concern.rb

module MyConcern
  extend ActiveSupport::Concern

  included do
    # common stuff here
  end

  class_methods do
    # define class methods here
  end
end
class MyModel < ActiveRecord::Base
  include MyConcern

  # ...
end
app/models/my_model.rb

module MyConcern
  extend ActiveSupport::Concern

  included do
    # common stuff here
  end

  class_methods do
    # define class methods here
  end
end
class MyModel < ActiveRecord::Base
  include MyConcern

  # ...
end
classmymodel

顺便说一句,您可以在
包含的
块和任何实例外方法中,以及
类方法中的
self
块中引用模型类。

您会注意到,您的标准rails项目在
模型
文件夹下包含一个名为
关注点
的文件夹

在该文件夹中创建一个模块,
image\u handling.rb

module ImageHandling
  extend ActiveSupport::Concern
  included do
    # ...(include all your common methods here)
  end
end
在你的公司里,新闻、个人资料模型称之为关注

class Company < ActiveReecord::Base
  include ImageHandling
  ...
end
class公司
gem'rails',4.2.5'您是否修改了
自动加载路径
?如果
include::upload
?不。我只做了示例。请粘贴文件
upload.rb
。模块Sta类应用程序spring
gem,是吗?如果是这样,您应该在停止服务器之后和重新启动服务器之前执行
spring stop
。这是在新创建的rails项目上。除了这个还有别的方法吗?我做不到。你有什么进展吗?您正在运行开发服务器,对吗<代码>轨道s
?您说过,在运行rails s之前,您运行了spring stop(弹簧停止)
关注点
是自rails 4以来默认启用的。如果您仍然在Rails3上,那么应该加载带有关注点的路径,请看一下这个要点示例