Tag: ruby

ActiveRecord :: Relation(嵌套属性)的未定义方法

结果:: ActiveRecord_Relation的未定义方法`results’:0x007f9ee69ad148> Quantifieds#指数 这里的第一行是导致exception的原因。 要重述这个问题,我如何定义date_value和result_value以使其在quantieds索引中起作用? 它们来源于:_form中的结果。 我正在使用cocoon来创建嵌套属性。 整个索引 (根据第一个答复响应更新) AVERAGE () INSTANCE () <span class="glyphicon glyphicon-plus" result %> [:month, :year], class: ‘date-select’ %> 我需要更改控制器中的任何内容吗? class QuantifiedsController < ApplicationController before_action :set_quantified, only: [:show, :edit, :update, :destroy] before_action :authenticate_user!, except: [:index, :show] def index @quantifieds = Quantified.joins(:results).all @averaged_quantifieds = current_user.quantifieds.averaged @instance_quantifieds = current_user.quantifieds.instance end def show end […]

在设计中生成有意义的用户名

我正在尝试让我的应用程序自动生成用作名称的用户名。 通常,它们将是用户的名字和姓氏一起添加,但是当已经存在具有相同名字和姓氏的用户时,它将为每个名称增加一个数字。 这是我创建的方法: def full_name first_name + ‘ ‘ + last_name end def user_name t_user_name = first_name.downcase.strip.gsub(‘ ‘, ”).gsub(/[^\w-]/, ”) + last_name.downcase.strip.gsub(‘ ‘, ”).gsub(/[^\w-]/, ”) user_name = t_user_name num = 1 while User.find_by_user_name(user_name).count > 0 num += 1 user_name = “#{t_user_name}#{num}” end end 我目前收到错误: undefined method `find_by_user_name’ 我认为哪个会自动运行? 我也试着在这篇文章中展示的例子: 生成唯一的用户名(omniauth + devise) 但我一直收到错误: Mysql2::Error: Unknown […]

OSX上的Ruby nubie – 无法超越rake db:migrate – get Bus Error

原始问题: 我是(ruby的新手)使用RVM在Mac OSX 10.6上管理我的ruby 这是我的mac OX信息: $ rvm info ruby-1.9.2-head@1.9.2-head-gemset: system: uname: “Darwin fillibuster-2.local 10.6.0 Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386 i386” bash: “/bin/bash => GNU bash, version 3.2.48(1)-release (i386-apple-darwin10.0)” zsh: “/bin/zsh => zsh 4.3.9 (i386-apple-darwin10.0)” rvm: version: “rvm 1.5.2 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]” ruby: interpreter: “ruby” version: […]

使用mongrel_rails在rails app上启动ruby时,为什么会出现错误

尝试使用rails应用程序启动ruby时,为什么会出现以下错误 mongrel_rails开始 ? C:\ RailsTest \ cookbook2> mongrel_rails启动 **警告:Win32不支持守护程序模式。 **守护,任何打开的文件都已关闭。 查看log / mongrel.pid和log / mongr el.log获取信息。 **启动Mongrel听取0.0.0.0:3000 C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin /../ LIB /杂种/吨 cphack.rb:12:在`initialize_without_backlog’中:每个套接字地址只有一个用法 通常允许使用ess(协议/网络地址/端口)。 – bind(2)(Errno :: EAD DRINUSE) 来自c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin /../ lib / mongrel / tcphack.rb:12:在`initialize’中 来自c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin /../ lib / mongrel.rb:93:在’new’中 来自c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin /../ lib / mongrel.rb:93:在`initialize’中 来自c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin /../ lib / mongrel / configurator.rb:139:在’new’中 来自c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin /../ lib […]

有没有办法在Ruby中清除“UTF-8中无效字节序列”错误的文件?

在发布到StackOverflow之前我已尝试过所有内容我真的希望有人可以提供帮助,但我非常绝望。 所以,我有一个服务,通过客户提供的XML提要将数据上传到我们的数据库。 这些XML文件通常声称是UTF-8编码,但显然有很多无效的字节序列。 我可以通过在导入之前运行以下Linux命令来清理这些文件并将它们完美地导入我们的数据库: tr -cd ‘^[:print:]’ clean.xml 只需运行这一个Linux命令,我就可以使用Rails中的Nokogiri将所有数据导入我的数据库。 问题是我们正在Heroku上部署,我无法使用Linux命令预处理该文件。 我花了上周的时间在Internet上搜索基于Rails的本地解决方案来解决这个问题,但是没有一个能够解决问题。 在我完成我尝试过的所有建议之前,这是我的原始代码: data_source = ARGV[0] data_file = open data_source data_string = data_file.read doc = Nokogiri::XML.parse(data_string) doc.xpath(“.//job”).each do |node| hash = node.element_children.each_with_object(Hash.new) do |e, h| h[e.name.gsub(/ /,”_”).strip.downcase.to_sym] = e.content data.push(newrow) end end 在原始文件上运行此操作会产生错误:“UTF-8中的字节序列无效” 以下是我尝试过的所有有用的建议,但都失败了。 使用编码器 Coder.clean!(data_string,“UTF-8”) 强制编码 data_string.force_encoding(’BINARY’)。encode(’UTF-8’,:undef =>:replace,:replace =>”) 转换为UTF-16并返回UTF-8 data_string.encode!(’UTF-16’,’UTF-8’,:invalid =>:replace,:replace =>”)data_string.encode!(’UTF-8’,’UTF-16’) 使用valid_encoding? data_string.chars.select […]

RSpec with devise – 仅记录第一个测试

我正在尝试为用户必须使用设备登录的页面编写rspecfunction测试。 RSpec.feature “User sees items” do before(:context) do .. login_as(@user, :scope => :user) end after(:each) do Warden.test_reset! end it “sees 3 items” do create_items 1, 2 visit root_path expect(page).to have_css(“#total”, text: “3”) end it “sees 7 items” do create_items 3, 4 visit root_path expect(page).to have_css(“#total”, text: “7”) end end 在我的rails_helper我include Warden::Test::Helpers 。 但只有我的第一次测试值3用户登录。第二次测试有设计错误You need to […]

迁移失败:PG :: ProtocolViolation Rails

我刚刚更新到rails4并意识到我的db角色和数据库不知何故没有设置。 所以我删除了数据库并重新创建了app_development数据库和角色。 现在我正在尝试运行rake db:migrate并且它失败并出现以下错误: == DeviseCreateUsers: migrating ============================================== — create_table(:users) -> 0.0081s — add_index(:users, :email, {:unique=>true}) -> 0.0031s — add_index(:users, :reset_password_token, {:unique=>true}) -> 0.0030s == DeviseCreateUsers: migrated (0.0144s) ===================================== rake aborted! An error has occurred, this and all later migrations canceled: PG::ProtocolViolation: ERROR: bind message supplies 1 parameters, but prepared statement “a1” requires 0 : […]

没有设置Rails 4会话cookie

我正在构建Rails 4应用程序。 它主要是json api所以我也在我的应用程序中使用rails-api gem。 我有一个扩展ActionController :: API的 ApiControllers。 现在我需要添加一些经典的服务器渲染HTML模板。 这些渲染的控制器扩展了ActionController :: Base 。 我有问题,rails没有在浏览器中设置cookie,所以我的会话根本不起作用。 我尝试在其中启动新的Rails 4应用程序和会话,一切正常。 我不知道为什么它在我的应用程序中不起作用。 缺少set-cookie标头 。 有任何想法吗?

Rspec:如何在私有方法中引发exception测试?

在私有方法中测试exception时出错。 如何在私有方法中引发exception,该方法是从公共方法调用的? 上市 def public_method private_method end 私人的 def private_method tries = 0 begin raise Product::StaleObjectError.new(“Product is changed while you were editing”) if stale_object? // Do some work raise Exception.new(“Total amount used is greater than approved”) if total_approved e if tries < MAX_RETRIES tries += 1 sleep(1 + tries) reload retry else raise Product::StaleObjectError("Product is […]

将Rails表单部分嵌入到另一个页面中

我正在构建一个带有联系我们页面的rails 4.2.0应用程序(此页面确实有一个半空控制器)。 我试图从另一个控制器嵌入一个表单部分。 这是代码(减去文本): 当我运行这个时,我得到错误’forms中的第一个参数不能包含nil或为空’。 我的查询表格看起来像一个基本的铁轨forms: prohibited this enquiry from being saved: 可能是错误的原因是什么? 或者是否有更好的方法将视图嵌入另一个视图? 更新/编辑: 这是路线: devise_for :users resources :rooms do resources :viewings end resources :rmcats resources :extras resources :extracats resources :enquiries root :to => redirect(‘/pages/home’) get ‘pages/home’ get ‘pages/contactus’ 和查询控制器: class EnquiriesController < ApplicationController before_action :set_enquiry, only: [:show, :edit, :update, :destroy] # GET /enquiries […]