说我有以下课程:
class Buyer < ActiveRecord::Base
attr_accesible :first_name, :last_name
Run Code Online (Sandbox Code Playgroud)
以及CSV文件中的以下内容:
First Name,Last Name
John,Doe
Jane,Doe
Run Code Online (Sandbox Code Playgroud)
我想将CSV的内容保存到数据库中.我在Rake文件中有以下内容:
namespace :migration do
desc "Migrate CSV data"
task :import, [:model, :file_path] => :environment do |t, args|
require 'csv'
model = args.model.constantize
path = args.file_path
CSV.foreach(path, :headers => true,
:converters => :all,
:header_converters => lambda { |h| h.downcase.gsub(' ', '_') }
) do |row|
model.create!(row.to_hash)
end
end
Run Code Online (Sandbox Code Playgroud)
结束
我得到了一个undefined method 'downcase' for nil:NilClass.如果我排除标题转换器,那么我得到unknown attribute 'First Name'.什么是对,比如说,从转换头的正确语法First Name来first_name?
我使用Carrierwave允许用户将图像附加到他们的帖子.在帖子列表中,我显示缩略图(以haml为单位),如下所示:
%td= image_tag post.image.url :thumb
Run Code Online (Sandbox Code Playgroud)
如果在创建新帖子时验证失败,我会像这样显示缓存:
= image_tag "/#{ImageUploader::cache_dir}/#{post.image_cache}"
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚的是如何显示缓存的缩略图.检查文件系统确认它与缓存的映像位于同一目录中.我试过了
= image_tag post.image_cache :thumb
Run Code Online (Sandbox Code Playgroud)
但它错误 wrong number of arguments (1 for 0)
考虑以下代码:
require 'active_support/concern'
module Inner
end
module Outer
extend ActiveSupport::Concern
included do
include Inner
end
end
class FirstClass
include Outer
end
module SecondInner
end
module SecondOuter
include SecondInner
end
class SecondClass
include SecondOuter
end
Run Code Online (Sandbox Code Playgroud)
为什么通过 AS::Concern 包含的模块的祖先顺序与普通的 Ruby 不同?
FirstClass.ancestors
# => [FirstClass, Inner, Outer, Object, PP::ObjectMixin, Kernel, BasicObject]
SecondClass.ancestors
# => [SecondClass, SecondOuter, SecondInner, Object, PP::ObjectMixin, Kernel, BasicObject]
Run Code Online (Sandbox Code Playgroud) 我在服务类中有一个创建对象的方法:
def createContent (fileName, description) {
def content = new Content(
fileName:fileName,
description:description,
).save()
}
Run Code Online (Sandbox Code Playgroud)
这些属性都不可为空.如何将验证错误传回显示?我尝试过flash.message和render,两者都不能在服务类中使用.我也尝试过.save(failOnError:true),它显示了很长的错误列表.
我在我的本地PC上克隆了一个repo,我希望在本地repo中创建一个目录结构.
我只是创建一个目录,然后将更改推回到远程,还是有特定的进程?