Tag: ruby on rails 3

如何使用Ruby将列标题写入csv文件?

我在使用Ruby将列写入csv文件时遇到问题。 下面是我的代码片段。 calc = numerator/denominator.to_f data_out = “#{numerator}, #{denominator}, #{calc}” File.open(‘cdhu3_X.csv’,’a+’) do|hdr| hdr << ["numerator","denominator","calculation\n"] #< column header hdr << "#{data_out}\n" end 代码将列标题添加到每一行,我只需要在每列数据的顶部。 我在这里和其他地方搜索过,但无法找到明确的答案。 任何帮助将不胜感激。

如何使用依赖::在rails中销毁?

我有2个型号,如下所述。 class EmpGroup < ActiveRecord::Base belongs_to :user has_many :emp_group_members, dependent: :destroy end 和 class EmpGroupMember < ActiveRecord::Base belongs_to :emp_group belongs_to :user end 现在问题是每当我试图销毁一个组然后我收到如下错误。 PG::ForeignKeyViolation: ERROR: update or delete on table “emp_groups” violates foreign key constraint “fk_rails_bd68440021” on table “emp_group_members” DETAIL: Key (id)=(1) is still referenced from table “emp_group_members”. 我错过了什么?

预计定义。 在模块内部调用类时

我是铁杆新手。 我在lib目录中有一个设置,如下所示: lib/ blog/ core/ search/ base.rb base.rb也定义了Base类: module Blog module Core module Search class Base attr_accessor :properties def initialize(params) @properties = {} end end end end end 我的application.rb中有以下代码 config.autoload_paths += Dir[“#{config.root}/lib/**/”] 当我将它包含在posts控制器中时,我会收到以下错误: LoadError in PostsController#index Expected /home/usr/code/blog/lib/blog/core/search/base.rb to define Base 任何的想法? 我正在使用rails 3.2.5和RVM。 谢谢你的建议。 更新:添加了我的完整堆栈: Started GET “/admin/posts” for 127.0.0.1 at 2012-06-08 21:06:18 +0800 […]

表格提交两次,原因是:remote => true

我的表单提交了两次,经过双重检查,它是由’:remote => true’引起的。 我删除它,我的项目运作良好。 谁能告诉我为什么? 以及如何使用’:remote => true’? 我的ruby代码: true, :id => ‘new_product_group_form’) do %> [:product_scopes, :groups, group_name]), scopes.keys.map do |scope_name| [ t(:name, :scope => [:product_scopes, :scopes, scope_name]), scope_name] end ] end ) %> <input type="submit" value="” /> 浏览器中的最终html代码。 Add a scope In Taxon(without descendants) In taxons and all their descendantsProduct name have following Product […]

nokogiri – 错误:无法构建gem原生扩展

我更新了Lion的开发者版本,并注意到我再也无法启动rails应用程序了。 每当我尝试sudo bundle install ,我都会收到以下错误: Installing nokogiri (1.4.4) with native extensions /Library/Ruby/Site/1.8/rubygems/installer.rb:551:in `build_extensions’: ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError) /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb mkmf.rb can’t find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ruby.h Gem files will remain installed in /Users/jamielawrence/Documents/Websites/atgdb/vendor/bundle/ruby/1.8/gems/nokogiri-1.4.4 for inspection. Results logged to /Users/jamielawrence/Documents/Websites/atgdb/vendor/bundle/ruby/1.8/gems/nokogiri-1.4.4/ext/nokogiri/gem_make.out from /Library/Ruby/Site/1.8/rubygems/installer.rb:504:in `each’ from /Library/Ruby/Site/1.8/rubygems/installer.rb:504:in `build_extensions’ from /Library/Ruby/Site/1.8/rubygems/installer.rb:180:in `install’ from […]

确定rails3 jquery自动完成插件的结果范围

我正在使用https://github.com/crowdint/rails3-jquery-autocomplete ,它似乎正在工作。 唯一的问题是假设我有一个字段在所有post标题上执行自动完成,但我希望它只显示用户创建的post标题。 有没有办法做这样的事情? 即find_by还是什么? 谢谢! -Elliot 编辑 它在文档中说: If you want to display a different version of what you’re looking for, you can use the :display_value option. This options receives a method name as the parameter, and that method will be called on the instance when displaying the results. class Brand < ActiveRecord::Base def […]

无法让ActionMailer通过SMTP与MS Exchange一起使用

这是我的简单测试程序(使用ActionMailer 3.0.8,Ruby 1.9.2p180 Mac OS X): require ‘rubygems’ require ‘action_mailer’ ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.smtp_settings = { :address => “my_exchange_server”, :port => 25, :domain => ‘my_domain.org’, :authentication => :login, :user_name => ‘my_user’, :password => ‘my_password’, :enable_starttls_auto => false } ActionMailer::Base.raise_delivery_errors = true ActionMailer::Base.perform_deliveries = true ActionMailer::Base.default :from => ‘from_email@my_company.com’ m = ActionMailer::Base.mail :to => ‘to_email@my_company.com’, :subject […]

在Rails中沉默弃用警告3

任何人都可以告诉我如何在Rails 3中抑制弃用警告? 我有一些情况下它会抛出误报。 即使用 – 来自haml的for循环和来自dynamic_form插件的f.error_messages。 谢谢

新安装的Rails + Devise总是获得401 Unauthorized

我有一个新的Rails安装,我正在尝试使用Devise设置身份validation。 据我所知,我有一个非常基本的设置应该可以工作,但每当我尝试使用默认的Devise登录表单登录时,我都会收到一个未经授权的错误。 我确信我的凭据是正确的,因为我创建了一个用户在控制台中进行测试,如下所示: User.new({:email=>’mark@markdavies.com.au’, :priv_level => ‘admin’, :password=>’mypassword’, :password_confirmation=>’mypassword’}).save 我的用户型号: class User < ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :confirmable # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me, :priv_level, :unconfirmed_email # attr_accessible […]

在Rails应用程序中记录所有方法调用

有没有一种简单的方法来记录Rails应用程序中的所有方法调用? 我的主要用途是测试(和调试测试)。 我想拥有更多的历史而不是堆栈跟踪(例如,使用’-b’选项运行rspec时)。