Tag: ruby on rails

渲染部分UI元素

在rails中,我经常需要声明在许多模型中使用的部分。 例如,有一个部分我想要一个包裹在按钮周围的表单,这样我就可以传入一个控制器,一个动作和一个对象,并将该对象发送到该控制器的动作。 现在这不是我的任何模型所特有的。 在模型的文件夹中声明这个部分文件(_processor_button.html.erb)似乎不合适。 你有什么建议是放置这个文件的最佳位置,以及如何从那里调用它,你建议采用一种完全不同的方式来接近上面提到的例子吗?

使用webpack从react-rails应用程序中的node_modules加载字体

我有一个使用webpacker设置的react-rails应用程序。 我正在尝试使用node_modules中的字体加载font-awesome-pro。 我认为这是一项微不足道的任务,但我似乎无法找到关于如何做到这一点的任何好的文档。 这是我到目前为止: package.json依赖项: “dependencies”: { “@rails/webpacker”: “3.5”, “babel-preset-react”: “^6.24.1”, “bootstrap”: “^4.1.3”, “prop-types”: “^15.6.2”, “react”: “^16.5.2”, “react-dom”: “^16.5.2”, “react-slick”: “^0.23.1”, “react_ujs”: “^2.4.4”, “slick-carousel”: “^1.8.1”, “tachyons-z-index”: “^1.0.9” }, “devDependencies”: { “@fortawesome/fontawesome-pro”: “^5.2.0”, “babel-plugin-transform-class-properties”: “^6.24.1”, “babel-preset-env”: “^1.7.0”, “file-loader”: “^2.0.0”, “path”: “^0.12.7”, “webpack-dev-server”: “2.11.2” } file.js: var path = require(‘path’); module.exports = { test: /\.(woff(2)?|eot|otf|ttf|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, exclude: path.resolve(__dirname, […]

check_auth_continue:504 5.7.4无法识别的身份validation类型(Net :: SMTPSyntaxError)

我有一个简单的脚本,通过ruby中的gmail smtp服务器发送邮件,现在我必须将脚本移动到微软交换服务器,我不断收到上述错误,请我帮忙谢谢, 我从irb尝试了下面的内容,它也因上述错误而失败: Net::SMTP.start(‘your.smtp.server’, 25, ‘mail.from.domain’,’Your Account’, ‘Your Password’, :login)

‘array.map’是否保留原始顺序?

我有一个User类has_many Job s。 我使用以下代码映射作业: def ranges user.jobs.map { |u| [u.start_at, u.end_at] } end 我有一个比较两个数组的规范: my_array = [[start1, end1], [start2, end2]] expect(ranges).to eq my_array 它传递给本地,但不是Bamboo构建。 我不得不改变它以通过构建。 我仍然不明白为什么它会像这样。 即使地图不保留订单,我也期待一些随机模式。 为什么这会传递给我的本地,但是在Bamboo版本中失败?

Ruby on Rails 4.我可以通过flash范围传递对象

我有一个控制器,需要将validation失败的对象传递给另一个控制器操作来处理它们并向用户显示: class PersonController def save_persons invalid_persons = … #array of Person.new objects flash[:invalid_persons] = invalid_persons redirect_to action: :fix_errors end def fix_errors invalid_persons = flash[:invalid_persons] invalid_persons.each do |invalid_person| puts invalid_person.errors #here i get exception! end end end 当我尝试puts invalid_person.errors我得到错误: undefined method errors for #Hash:0x007fd594e79098> 。 似乎rails将我的对象数组转换为一些哈希值 你能建议我,通过flash传递一些物体的正确方法是什么?

Ruby / Rails:遍历文件夹并解析元数据到种子DB

我有一堆文档,我想在Rails应用程序中编制索引。 我想使用各种rake任务来梳理目录层次结构,寻找文件并从这些文件中捕获元数据以在Rails中编制索引。 我不确定如何在Ruby中执行此操作。 我找到了一个名为pdftk的实用程序,它可以从PDF文件中提取元数据(我索引的大部分内容都是PDF),但我不确定如何捕获该数据的各个部分? 例如,要获取下面的ModDate或每个BookmarkTitle和BookmarkPageNumber 。 具体来说,我想遍历文件层次结构,为我找到的每个.pdf执行pdftk $filename dump_data命令,然后将该输出的重要部分捕获到rails模型中。 pdftk的输出: $ pdftk BoringDocument883c2.pdf dump_data InfoKey: Creator InfoValue: Adobe Acrobat 9.3.4 InfoKey: Producer InfoValue: Adobe Acrobat 9.34 Paper Capture Plug-in InfoKey: ModDate InfoValue: D:20110312194536-04’00’ InfoKey: CreationDate InfoValue: D:20110214174733-05’00’ PdfID0: 2f28dcb8474c6849ae8628bc4157df43 PdfID1: 3e13c82c73a9f44bad90eeed137e7a1a NumberOfPages: 126 BookmarkTitle: Alternative Maintenance Techniques BookmarkLevel: 1 BookmarkPageNumber: 3 BookmarkTitle: CONTENTS BookmarkLevel: 1 […]

AdminController中的AbstractController :: DoubleRenderError

我正在进行狂欢商务,我正在尝试调整active_sale_controller中的几个动作。 我有一些条件,如果条件失败,我会重定向(后退),否则我将继续下一步。 我现在面临的问题是我在同一个动作中使用了redirected_to(:back)两次,并且在同一个动作中我还有一个redirected_to到另一个控制器,浏览器显示一个错误,表示 “Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do […]

Ruby on Rails中的嵌套URL,控制器和视图

我想要做的是将我的页面嵌套在Rails中,例如: www.mysite.com/fifa17/ps4 这样,一旦我在show.html.erb上 ,我就可以使用该路线在该控制台平台上专门为该游戏销售我的产品。 目前,我有一个带有has_many :games的Console模型和带有has_many :games的Game模型,但我无法使路由和视图工作。 我只有一个或两个控制器吗? 我有两个name ActiveRecords的Games和Consoles 。 我似乎能够调用其中一个操作(即Console.name ),但不能同时调用控制台名称和游戏名称。

Ruby on Rails – 将方法的返回值传递给has_attached_file。 我的Ruby语法错了吗?

我正在编写我的第一个RoR应用程序,目前我正致力于允许用户上传图像。 我正在使用Paperclip来达到这个目的。 其中一个步骤涉及将has_attached_file添加到我的模型中: class MyModel “, small: “60×75#>” } #… end 如果我这样做,一切顺利(或者似乎)。 但是我还需要在其他地方访问与整数相同的常量值,所以我添加了一个哈希: class MyModel “, small: “60×75#>” } def picture_sizes { large: {width: 120, height: 150}, small: {width: 60, height: 75} } end #… end 这会产生丑陋的冗余。 所以我考虑编写一个从第二个哈希生成第一个哈希的方法,就像这样 class MyModel ” end return result end #… end 但这会引发一个错误: undefined local variable or method `picture_sizes_as_strings’ for […]

使用RSpec测试奇异资源控制器

我在routes.rb定义了一个奇异的资源,看起来像这样 Rails.application.routes.draw do resource :dog, only: [:create], to: “dog#create”, controller: “dog” end 之后我用这样的创建动作定义了一个控制器 class DogController < ApplicationController def create render json: {}, status: :ok end end 而现在我正试图用这样的RSpec来测试它 require “rails_helper” describe DogController do it “works” do post :create, params: { foo: :bar } end end 这是抛出此错误而不是传递: ActionController::UrlGenerationError: No route matches {:action=>”create”, :controller=>”dog”, :foo=>:bar} 我究竟做错了什么?