簡體   English   中英

從Rails 3.1引擎訪問模型

[英]Accessing a model from a Rails 3.1 Engine

我在最后一天一直在和它搏斗,它正在驅使我努力!

作為一個學習練習,我決定將一些代碼打包成Rails Gem。 這段代碼有一個控制器動作,一個路由,一個模型和一個幫助器,所以我認為最合適的創建Gem的方法是將它創建為一個Rails引擎。

除了一件事,一切似乎都運作良好。 當我嘗試從Controller或Views(使用引擎的應用程序)中引用模型時,例如:

@su = Shortener::ShortenedUrl.generate("http://stackoverflow.com")

我收到以下錯誤:

uninitialized constant Shortener::ShortenerHelper::ShortenedUrl

這很奇怪,因為當我從項目控制台執行代碼時,不會發生錯誤。 我認為這是因為我已將所有代碼放入“Shortener”命名空間/模塊中。 我這樣做是為了避免在其他應用程序中使用時發生沖突。

代碼文件層次結構如下所示:

項目目錄/文件列表的圖像

這里是有問題的重要文件的類/模塊聲明代碼(刪除了內容)

應用程序/控制器/縮短器/ shortened_urls_controller

module Shortener
  class ShortenedUrlsController < ::ApplicationController

    # find the real link for the shortened link key and redirect
    def translate
      # convert the link...
    end
  end
end

應用程序/模型/縮略服務/ shortened_urls

module Shortener
  class ShortenedUrl < ActiveRecord::Base

    # a number of validations, methods etc  

  end 
end

應用程序/佣工/縮略服務/ shortener_helper

module Shortener::ShortenerHelper

  # generate a url from either a url string, or a shortened url object
  def shortened_url(url_object, user=nil)

    # some code to do generate a shortened url

  end

end

LIB /縮短器/ engine.rb

require "rails/engine"
require "shortener"

module Shortener

  class ShortenerEngine < Rails::Engine

  end

end

LIB / shortener.rb

require "active_support/dependencies"

module Shortener

  # Our host application root path
  # We set this when the engine is initialized
  mattr_accessor :app_root

  # Yield self on setup for nice config blocks
  def self.setup
    yield self
  end

end

# Require our engine
require "shortener/engine"

shortener.gemspec

require File.expand_path("../lib/shortener/version", __FILE__)

# Provide a simple gemspec so you can easily use your enginex
# project in your rails apps through git.
Gem::Specification.new do |s|
  s.name                      = "shortener"
  s.summary                   = "Shortener makes it easy to create shortened URLs for your rails application."
  s.description               = "Shortener makes it easy to create shortened URLs for your rails application."
  s.files                     = `git ls-files`.split("\n")
  s.version                   = Shortener::VERSION
  s.platform                  = Gem::Platform::RUBY
  s.authors                   = [ "James P. McGrath" ]
  s.email                     = [ "gems@jamespmcgrath.com" ]
  s.homepage                  = "http://jamespmcgrath.com/projects/shortener"
  s.rubyforge_project         = "shortener"
  s.required_rubygems_version = "> 1.3.6"
  s.add_dependency "activesupport" , ">= 3.0.7"
  s.add_dependency "rails"         , ">= 3.0.7"
  s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
  s.require_path = 'lib'
end

我在GitHub上發布了引擎的完整代碼:

https://github.com/jpmcgrath/shortener

注意:此引擎有一個生成器,用於生成所需的遷移文件。 類型:

rails g shortener

我還創建了一個展示問題的rails 3.1 app(查看項目控制器的第18行):

https://github.com/jpmcgrath/linky

有任何想法嗎? 我已經在網上搜索過,但是還沒有找到任何真正明確的制作引擎寶石的指南。 任何助手都會非常感激。

謝謝!

在您的引擎助手( app/helpers/shortener/shortener_helper.rb )中,將ShortenedUrl兩次出現替換為Shortener::ShortenedUrl

我在開始時發現這個錯誤很奇怪,因為ruby應該在封閉模塊中尋找常量。 但是幫助程序包含在另一個類中,這可能意味着常量名稱解析環境與您在文件中看到的環境不同。

如果您想了解有關命名空間引擎及其行為的更多信息,可以查看這個優秀的答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM