Rails 4:如何使用includes()和where()来检索关联对象

我无法弄清楚如何使用.where()方法来检索关联的模型数据。 在此示例中,Projects belongs_to Users … class Project < ActiveRecord::Base belongs_to :user has_many :videos end class User < ActiveRecord::Base has_many :projects end class ProjectsController params[:id]} ).first end end 在App / views / projects / invite.html.erg 返回: — !ruby/object:Project attributes: id: 22 name: Some Project Name belongs_to: 1 instructions: Bla bla bla active: true max_duration: 2 max_videos: […]

Ruby Post请求:PDF文件

上传文档的API示例是这样的 headers = { ‘Authorization’: ‘Bearer ACCESS_TOKEN’, } params = { ‘doctor’: ‘https://drchrono.com/api/doctors/1234’, ‘patient’: ‘https://drchrono.com/api/patients/5678’, ‘description’: ‘Short document description here’, ‘date’: ‘2014-02-24’, ‘metatags’: json.dumps([‘tag1’, ‘tag2’]), } with open(‘/path/to/your.pdf’, ‘rb’) as f: files = {‘document’: f} requests.post( ‘https://drchrono.com/api/documents’, data=params, files=files, headers=headers, ) 我使用Prawn来创建PDF。 一个路由自动下载PDF,而另一个路由使其在浏览器中查看。 我遇到了问题(试图弄清楚它是Prawn PDF问题还是PDF问题)我从网上下载了一个相当基本的PDF。 同样的问题。 我正在使用HTTParty发送我的POST请求。 headers = {‘Authorization’ => ‘Bearer ‘ + access_token} […]

Ruby – 使用雾将内容附加到现有s3文件的末尾

如何在S3中的现有或新创建的文件中附加文本。 我正在使用fog ,我有以下代码 require ‘fog’ file = “abc.csv” bucket = ‘my_bucket’ storage = Fog::Storage.new(:provider => ‘AWS’, :aws_access_key_id => ‘XXXXXXXX’, :aws_secret_access_key => ‘YYYYYYYY’) dir = connection.directories.new(:key => bucket) # no harm, if this bucket already exists, if not create one buffer = [“big_chunk1”, “big_chunk2”, “big_chunk3”, “big_chunk4”, “big_chunk5”] # I need help after this line. No changes […]

使用rails-Ajax调用控制器方法?

我试图从我的视图中的按钮在我的application_controller.rb中执行Ruby方法。 在昨天的post中,有人告诉我使用Ajax调用这样做,因为没有它,只会在页面加载时运行。 我对此很陌生,我很难理解它。 我安装了rails-Ajax gem,它被添加到我的Gem文件中。 我遵循“ 将Rax网站的Ajaxfunction集成到历史记录,书签,部分刷新,Rails闪存,用户回调,脚本执行,重定向。 ”直到第5步。 我只是不知道接下来要做什么。 这是我目前的配置。 该方法仅在页面加载时运行: 我的看法: executer function executeit() { var selectingCommand = document.getElementById(“CommandSelect”); var selectedCommand = selectingCommand.options[selectingCommand.selectedIndex].text; var selectingServer = document.getElementById(“serverlist”); var selectedServer = selectingServer.options[selectingServer.selectedIndex].text; var username=document.getElementById(“login”).text; var password=document.getElementById(“password”).text; ; } 我在application_controller.rb中的方法: def execute require ‘rubygems’ require ‘net/ssh’ @hostname = “smtlmon02” @username = “test” @password = “1234” @cmd […]

如何在ruby中实现curry(部分函数)

我需要一些在ruby中实现curry函数的例子(1.8.6或1.8.7而不是1.9)。

为什么一些正则表达式引擎在单个输入字符串中匹配。*两次?

许多正则表达式引擎在单行字符串中匹配.* 两次 ,例如,在执行基于正则表达式的字符串替换时: 根据定义,第一个匹配是整个(单行)字符串,如预期的那样。 在许多引擎中有第二个匹配,即空字符串 ; 也就是说,即使第一个匹配消耗了整个输入字符串, .* 再次匹配,然后匹配输入字符串末尾的空字符串。 注意:要确保只找到一个匹配项,请使用^.* 我的问题是: 这种行为有充分的理由吗? 一旦输入字符串被完全消耗,我就不会期望再次尝试找到匹配项。 除了试验和错误之外,您是否可以从文档/正则表达式方言/标准中收集哪些引擎表现出这种行为? 更新 : revo的有用答案解释了当前行为的方式; 至于潜在的原因 ,请参阅此相关问题 。 表现出行为的语言/平台: # .NET, via PowerShell (behavior also applies to the -replace operator) PS> [regex]::Replace(‘a’, ‘.*’, ‘[$&]’ [a][] # !! Note the *2* matches, first the whole string, then the empty string # Node.js $ node […]

有没有办法用Rspec存根包含模块的方法?

我有一个包含在另一个模块中的模块,它们都实现了相同的方法。 我想存根包含模块的方法,如下所示: module M def foo :M end end module A class << self include M def foo super end end end describe "trying to stub the included method" do before { allow(M).to receive(:foo).and_return(:bar) } it "should be stubbed when calling M" do expect(M.foo).to eq :bar end it "should be stubbed when calling A" do […]

从ActiveRecord获取表名

我使用ActiveRecord::Base.set_table_name在动态创建的ActiveRecord类上设置我的表名。 现在我需要知道如何在以后获得该值。 api文档没有提到任何关于如何执行此操作的内容。 另外,我无法从ActiveRecord类名派生表名,因为它们的键控方式与表名不同。 这是我正在做的更好的例子 table_klass = Class.new(ActiveRecord::Base) ActiveRecord::Base.const_set(const_name,table_klass) app = @app table_klass.class_eval do after_save do @@channel.push self end set_table_name t.server_table establish_connection( :adapter => “mysql2”, :host => app.db_host, :username => app.db_user, :password => app.db_pass, :database => app.db_name ) end 在这种情况下,如果const_name = Test并且数据库名称是Database,那么它应该创建一个ActiveRecord :: Base :: DatabaseTest类。 但是当我调用table_name时,我得到了未定义的局部变量或方法。 我需要在类上调用table_name吗? 更新:我通过调用instance.class.table_name

Rails资产管道和摘要值

有谁知道资产摘要值究竟是如何计算的? 如果我有两个JS文件,其中包含各种其他包含的JS脚本,那么如果没有更改内部脚本,那么每个文件是否会保持相同的摘要哈希值? 或者是每次资产:运行预编译操作时计算的新摘要值?

无法加载此类文件 – libxml_ruby for Windows

我的步骤是, 执行RubyInstaller.exe文件 安装ruby 安装路径:c:\ ruby​​193 安装DevKit gem install libxml-ruby –platform x86-mswin32-60 make test.rb文件 需要’rubygems’ 要求’xml’ ruby test.rb 打印错误消息 错误信息: C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’: 126: The specified module could not be found – C:/Ruby193/lib/ruby/gems/1.9.1/gems/lib xml-ruby-1.1.3-x86-mswin32-60/lib/libxml_ruby.so (LoadError) from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from C:/Ruby193/lib/ruby/gems/1.9.1/gems/libxml-ruby-1.1.3-x86-mswin32-6 0/lib/libxml.rb:9:in `’ from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from C:/Ruby193/lib/ruby/gems/1.9.1/gems/libxml-ruby-1.1.3-x86-mswin32-6 0/lib/xml.rb:11:in `’ from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require’ from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in […]