编辑:正如@Justin说的那样,当我试图找到一个甚至没有意识到的解决方案时,我可能搞砸了,oopsy ......将params更改为params.require(:pin).permit(:image,:description)确实解决了它.
虽然它现在不起作用,因为我得到一个"有一个与其内容不匹配的扩展名.
我正在关注一个月的Rails并尝试将上传图片放入脚手架工作中.我正在使用paperclip文件和simple_form.这是我认为相关的代码(随意提出要求):
_pin_form.erb.html
<%= simple_form_for(@pin, :html => {:multipart => true}) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.file_field :image, :label => "Upload an image" %>
<%= f.input :description, as: :text %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
Run Code Online (Sandbox Code Playgroud)
pin.rb
class Pin < ActiveRecord::Base
has_attached_file :image
belongs_to :user
validates :description, :presence => true
validates :user_id, :presence => true
validates_attachment :image, :presence => true
# Validate content type
validates_attachment_content_type :image, :content_type => /\Aimage/
# Validate filename …Run Code Online (Sandbox Code Playgroud) 我正在用javascript构建一个国际象棋游戏,并且对使用继承的正确方法有点不确定.在代码的一部分,我有一个片段对象,不同的片段类型扩展它,例如与骑士(因为它是最短的)它看起来像这样(没有评论):
件
/************* piece ********************/
function Piece(square, color) {
this.square = square;
this.color = color;
}
Piece.prototype.get_path = function(to) {
return null;
};
Piece.prototype.get_capture_path = function(to) {
return this.get_path(to);
};
Piece.prototype.isAt= function(square) {
return (this.square.equals(square));
};
/************* KNIGHT *****************/
Knight.prototype = Object.create(Piece.prototype);
Knight.prototype.constructor = Knight;
function Knight(square, color) {
Piece.call(this, square, color);
this.type = KNIGHT;
}
Knight.prototype.get_path = function(to) {
var h_movement = Math.abs(this.square.file - to.file);
var v_movement = Math.abs(this.square.rank - to.rank);
if ((h_movement === 2 && v_movement === 1) …Run Code Online (Sandbox Code Playgroud) 我有以下Java代码
public class Base {
private static boolean goo = true;
protected static boolean foo() {
goo = !goo;
return goo;
}
public String bar = "Base:" + foo();
public static void main(String[] args) {
Base base = new Sub();
System.out.println(base.bar);
}
}
public class Sub extends Base {
public String bar = "Sub:" + foo();
}
Run Code Online (Sandbox Code Playgroud)
而且我被问到它会打印什么.经过测试,答案似乎是Base:false,但我真的无法理解为什么不是Sub:true.
它显示了具有两个同名变量的基数!一个有印刷的基础:假,另一个是预期的(由我)Sub:true.实际上foo()被调用两次但每次实例化一个不同的变量?不应该在子类中创建具有相同名称的变量(并在创建第一个之后初始化)覆盖父类中的变量吗?Java如何选择打印哪一个?
我正在尝试编写一个程序,该程序使用按字母顺序排列的货架数据库作为键,以及可以从中创建的单词列表作为值。例如:
db['mnoo'] = ['moon', 'mono']
Run Code Online (Sandbox Code Playgroud)
因此,我编写了一个函数,该函数接受文件名并将其加载到文件架中。第一部分,将文件转换为具有与货架相同布局的字典,但货架部分需要很长时间。
我正在尝试使用〜100k条目的字典,每个值都是一个列表。每1000个输入项似乎要花费15-20秒,每个输入项似乎要占用约1kb的空间。这是无情的吗?
代码:
def save_to_db(filename, shelve_in='anagram_db'):
dct = anagrams_from_list(process_file(filename))
with shelve.open(shelve_in, 'c') as db:
for key, wordlist in dct.items():
if not key in db:
db[key] = wordlist
else:
db[key].extend(wordlist)
Run Code Online (Sandbox Code Playgroud)
编辑:只是快速的澄清:字典中的每个列表大约是1-3个字长,不应太大