Tag: ruby on rails 5

Rails:从控制器中的lib文件夹加载自定义类

我已经创建了一个lib / services / my_service.rb文件。 # /lib/services/my_service.rb class MyService … end 我想在app / controllers / my_controller中使用它 class MyController < ApplicationController def method service = MyService.new() end 我收到的错误是MyService是一个未初始化的常量。 我试着用它导入它 require ‘/lib/services/my_service.rb’ 但我得到了 cannot load such file — /lib/services/my_service.rb 编辑:我已尝试使用application.rb进行自动加载 config.autoload_paths << Rails.root.join('lib') 但没有骰子。 仍未获得未初始化的常量MyController :: MyService

Prawn – 在text_box展开后向下移动光标

我正在使用Prawn Gem生成PDF,我无法找到一种方法在text_box从溢出文本扩展后将cursor向下移动,类似于常规text调用的方式。 Text_box示例 pad(5) { text_box payable, :at => [bounds.left, cursor], :width => 540, :height => 15, :overflow => :expand, inline_format: true } move_down(15) pad(5) { text_box address, :at => [bounds.left, cursor], :width => 250, :height => 15, :overflow => :expand, inline_format: true text_box city, :at => [250, cursor], :width => 100, :height => 15, […]

通过Actioncable使用群聊查找与聊天室关联的消息用户的图像

对基本问题道歉。 我设法遵循教程,并在SO社区的帮助下设法与Action Cable建立群聊,并学习了这样做的负担。 但是,我正在尝试使用特定的html页面 – 与current_users Chatrooms相关联的Messaging User的图像。 我已经能够拉出与当前用户相关的聊天室以及这些聊天室中提供的最后一条消息。 我试过以下但这只给了我当前聊天室中我的消息用户的图像。 我在下面列出了所有相关代码。 show.html.erb(在我的聊天室html文件夹中) Chatroom.rb class Chatroom {where(direct_message: false) } scope :direct_messages, ->{ where(direct_message: true) } def self.direct_message_for_users(users) user_ids = users.map(&:username).sort name = “#{user_ids.join(” and “)}” if chatroom = direct_messages.where(name: name).first chatroom else chatroom = new(name: name, direct_message: true) chatroom.users = users chatroom.save chatroom end end end […]

Rails – 从Rails配置中获取名称空间常量

假设我有几个搜索引擎的实现。 module Searcher module Engine class Elasticsearch end class Algolia end end end 我想要一个每环境搜索引擎配置。 我在我的环境中声明了配置(对于某种桥模式) Rails.application.configure do config.search_engine = :elasticsearch end (我在特定环境文件中覆盖) 然后在我的搜索控制器中,我想根据我放在Rails.configuration.search_engine中的符号加载适当的类 @search_engine = Searcher::Engine::XXX @search_engine.search 如何从符号:elasticsearch解析完整的命名空间常量Searcher::Engine::XXX ? 编辑: Searcher::Engine.const_get(Rails.configuration.search_engine.to_s.titleize) 不起作用:它加载Elasticsearch (来自gem)而不是Searcher::Engine::Elasticsearch (我自己的组件) ::Searcher::Engine::Elasticsearch # => Searcher::Engine::Elasticsearch Searcher::Engine.const_get(Rails.configuration.search_engine.to_s.titleize) # => Elasticsearch Object.const_get(“::Searcher::Engine::#{Rails.configuration.search_engine.to_s.titleize}”) # => Elasticsearch Ruby 2.3.0 Rails 5

Rails 5同时运行puma和简单的xmlrpc服务器

我的rails应用程序需要一个XMLRPC服务器。 但是当我尝试时: rails s 只有XMLRPC服务器启动而且puma无法启动。 我在config.rb文件中所做的是: require_relative ‘config/environment’ require “xmlrpc/server” server = XMLRPC::Server.new( 1234 ) server.add_handler(‘my_test.test’) { |msg| “responce for #{msg}” } server.serve # HERE IS WHERE APP STUCKS WITH XMLRPC run Rails.application 有没有想法执行此操作?

As1851Tests中的NoMethodError#new with shallow routes

我有一个使用浅层资源嵌套的rails 5应用程序。 form_for已停止工作,我找不到原因。 该应用程序有多个嵌套模型,在根据我之前在此处提出的问题的建议更改为浅路线之前有效 我的架构如下: DB / schema.rb: create_table “as1851_tests”, force: :cascade do |t| t.bigint “damper_id” t.boolean “AS1851A”, default: false, null: false t.boolean “AS1851B”, default: false, null: false t.boolean “AS1851C”, default: false, null: false t.boolean “AS1851D”, default: false, null: false t.boolean “AS1851E”, default: false, null: false t.boolean “Pass”, default: false, null: false t.string “comment” t.datetime […]

如果所有子模块都标记为完整导轨,则将课程标记为自动完成5

编辑#1 这就是我现在拥有的 course_module.rb class CourseModule { where(complete: true) } after_save :course_completed private def course_completed course = course_module.course course.update(complete: true) if course.course_modules.all?(&:complete?) end end 名称匹配,所以没有问题,但是,我将如何调用模型方法? 原始问题 目前,我有课程,在课程中我有模块都有完整的列,这是一个布尔,目前,学生可以通过点击勾选完成模块,所以 我有这样的课程数据库 和模块表一样 但我希望function能够工作,所以当最后一个模块标记为完成时,课程本身在数据库中标记为完成,目前在course_modules_controller.rb我有 def complete @course_module = CourseModule.friendly.find(params[:id]) @course_module.complete = true @course_module.save redirect_to courses_path, notice: ‘Module completed, congratulations!’ end 这适用于模块,但如果最后一个任务已标记为完成,我想将课程标记为完成。

Rails 5测试的弃用警告

我正在尝试从rails 4.2.6更新到rails 5.0.0.1,但在运行我的测试时会收到以下警告: 弃用警告:ActionController :: TestCase HTTP请求方法在将来的Rails版本中只接受关键字参数。 例子: get:show,params:{id:1},session:{user_id:1} process:update,method :: post,params:{id:1}(从/ home / ubuntu / workspace / my_app /中的块调用测试/控制器/ tractions_controller_test.rb:25) 我一直在进行适当的更改以适应这种变化。 但是对于下面的集成测试行,我不知道如何正确更改它: delete api_sessions_path, { api_session: { org_id: nil } }, { “HTTP_API_TOKEN”: “token”, “HTTP_USER_EMAIL”: @user.email } 所以测试通过但是有弃用警告。 如果我将其更改为下面的两个选项,则测试将失败并出现语法错误。 如何正确调整线? delete api_sessions_path, params: { api_session: { org_id: nil } }, { “HTTP_API_TOKEN”: “token”, “HTTP_USER_EMAIL”: […]

Rails中的用户会话认证5

我正在尝试使用Rails 5中的操作电缆实现通知。 阅读Action电缆教程后,该电缆在会话和基于Cookie的身份validation上工作,以接收当前登录用户的通知。 module ApplicationCable class Connection < ActionCable::Connection::Base identified_by :current_user def connect self.current_user = find_verified_user logger.add_tags 'ActionCable', current_user.username end protected def find_verified_user if verified_user = env['warden'].session(:user) verified_user else reject_unauthorized_connection end end end 在find_verified_user中,我无法从会话中获取用户对象。 任何人都可以帮助我,在行动电缆中validation用户。

Rails 5查询从我正在查看的post以外的相同类别中提取post

我正在尝试在post详情页面上显示来自同一类别的3个其他post。 所以我有这个问题: def show @posts_from_same_category = Post.where(category_id: @post.category_id).limit(3).order(“RANDOM()”) end 所以页面结构如下: post标题 post文字 (标题)来自同一类别的更多post 其他职位名称1 其他职位名称2 其他职位名称3 它返回所有post,包括我正在查看的post。 我如何编写查询来拉除我当前正在查看的post以外的post? 谢谢