Tag: 模块

如何使用Rails在Heroku雪松堆栈上安装python模块

我们在Heroku Cedar堆栈上运行了一个rails应用程序,我们需要一个外部python模块,即’pyPdf’。 不幸的是,它不是预先安装的python模块之一。 有没有办法在Cedar堆栈上安装python模块? 我试图在我的应用程序的根目录添加requirements.txt,但这似乎不起作用。 可能是因为Heroku在看到我们的Gemfile后将其解散了。 任何帮助赞赏。

获取模型中当前用户详细信息的好方法是什么?

我在我的rails应用程序中使用了devise gem进行身份validation。 我想在after_create回调中获得有关当前用户的某些信息。 这怎么可能? 我的型号代码是: class Department field :name after_create :create_news private def create_news @user = current_user end end 但是当我创建新部门时,我得到了以下错误。 undefined local variable or method `current_user’ for # 最好的方法是什么?

如何从我的Rails模型中正确包含模块和调用模块函数?

我有一个模型,Show和一个模块Utilities class Show < ActiveRecord::Base include Utilities … def self.something fix_url("www.google.com") end end My Utilities文件位于lib / utilities.rb中 module Utilities def fix_url(u) !!( u !~ /\A(?:http:\/\/|https:\/\/)/i ) ? “http://#{u}” : u end end 但是当我在show class中调用它时,Rails会为“fix_url”抛出一个NoMethodError。 在模型中包含模块时,是否必须做一些不同的事情? 谢谢!

我如何需要ActiveSupport的rescue_from方法?

我在application controller有这个代码: # Method to capture and handle all exceptions rescue_from Exception do |ex| Rails.logger.debug ex do_stuff(ex) end 我想把它移到一个模块然后: class ApplicationController < ActionController::Base include 'module' … 现在我的模块看起来像: # lib/exception_mailer.rb require ‘action_mailer’ require ‘active_support’ module ExceptionMailer # Method to capture and handle all exceptions rescue_from Exception do |ex| … 我得到: undefined method ‘rescue_from’ for ExceptionMailer:Module 我用Google搜索了“我如何在模块中包含rescue_from?” […]

首先调用控制器,定义常量,第二次调用“未初始化的常量Oauth”?

我试图让OAuthgem与Rails 3一起工作,我遇到了这个奇怪的问题…(独立于gem,我想我之前遇到过这个问题) 我有一个名为“OauthTestController”的控制器,以及一个名为“ConsumerToken”的模型。 模型看起来像这样。 require ‘oauth/models/consumers/token’ class ConsumerToken < ActiveRecord::Base include Oauth::Models::Consumers::Token end 当我去“/ oauth_test / twitter”时,它会加载Oauth::Models::Consumers::Token模块,我可以连接到twitter没问题。 但是我第二次尝试它(只刷新/oauth_test/twitter url),它给了我这个错误: NameError (uninitialized constant Oauth): app/models/consumer_token.rb:4 app/models/twitter_token.rb:2 app/controllers/oauth_test_controller.rb:66:in `load_consumer’ 这是为什么? 它与加载路径或处于开发模式有关吗?

如何在模块中使用’before_action’

我想在模块中使用’before_action’。 不幸的是,我无法让它发挥作用。 我是googleing,但我发现的一切都无法解决问题。 我的模块文件如下所示: module ShowController include SimpleController #before_action :set_object, only: [:show] def show set_object end end 我想使用outcommented before_action行而不是show方法。 因此,我试图包括以下模块: include AbstractController::Callbacks include ActiveSupport::Callbacks include ActiveSupport::Concern include ActiveSupport 另外,我试图“要求’active_support / all’”或core_ext。 我收到的error_message是: undefined method `class_attribute’ for SimpleController::ShowController:Module 最后,没有任何结果,我没有找到解决方案。

如何在factory_girl工厂中包含一个模块?

我正在尝试在我的所有工厂中重用一个辅助方法,但是我无法让它工作。 这是我的设置: 辅助模块(在spec / support / test_helpers.rb中) module Tests module Helpers # not guaranteed to be unique, useful for generating passwords def random_string(length = 20) chars = [‘A’..’Z’, ‘a’..’z’, ‘0’..’9′].map{|r|r.to_a}.flatten (0…length).map{ chars[rand(chars.size)] }.join end end end 工厂(在spec / factories / users.rb中) FactoryGirl.define do factory :user do sequence(:username) { |n| “username-#{n}” } password random_string password_confirmation { […]

Rails找不到与Ruby类同名的模型

我是Ruby on Rails的新手,我有一个带有“Set”模型的项目。 这是在Rails 2.3.2中。 现在问题是它根本找不到该模型类的任何方法。 例如:“ find’ for Set:Class” or “undefined method未定义方法find’ for Set:Class” or “undefined method #的find’ for Set:Class” or “undefined method错误”。 它似乎试图在Ruby“Set”类而不是我的模型类中找到这些方法。 如果我可以像Module :: Set那样编写我的Set模型类的完全限定名称,它可能会工作,但我不确定那是什么。 (是的,我确实想要我的模型名称Set。在我的应用程序的上下文中,任何其他东西都会很尴尬)。 有任何想法吗?

从Controller调用模块function(NoMethodError)

所以我有一个模块“MiddleMan”我可以在rails控制台中调用它,但在控制器中我得到一个NoMethodError 在控制器中我有: class SignUpController < ApplicationController include MiddleMan def page_one @package = MiddleMan::read_catalog("a", "b", "c") end end 在middleman.rb模块中,我有: module MiddleMan def read_catalog(package, payment, coupon) Package.new(:price => “4.99”) end end 有什么想法吗?

Ruby on Rails:我应该在哪里存储模块?

我是Ruby on Rails的新手,我的问题是关于应用程序设计,基于Rails 3.在Rails上创建标准网站(如博客)的互联网上有很多数据,但我的应用程序需要的不仅仅是“发布和阅读“我不知道如何实现这一点。 想法: “Route”模型包括许多航空公司模块:“Ryanair”,“easyJet”等。 “Route.Update”方法在每个航空公司模块上调用“UpdateRoutes”(例如,“Ryanair.UpdateRoutes”,“easyJet.UpdateRoutes”) 对于更多模型(例如“Flight.find”)和更多航空公司(“Delta.FindFlights”),它应该以相同的方式工作 问题: 我应该在哪里存储所有模块? 我在Rails中看不到任何app / modules文件夹。 如果我的模块需要gem,我应该将它们包含在模块中还是模型中(实际使用它们的地方)? 我想让我的应用程序可扩展。 例如,我想添加一个新的工作航空公司(模块)而不更改“路线”,“飞行”或任何其他模型中的任何代码。 我想象类似于“IncludeAirlines”的方法,它将通过模块/航空公司/ 名称 .rb,包括每个模块并调用它所需的方法(例如名称 .UpdateRoutes)。 有没有更好的方法在Ruby on Rails中实现它?