这不是Rails特有的 - 我只是以Rails为例.
我在Rails中有一个模型:
class Item < ActiveRecord::Base
def hello
puts "Hello, #{self.name}"
end
end
Run Code Online (Sandbox Code Playgroud)
(假设Item模型(类)有一个叫做的方法name).我什么时候需要使用self.name,何时可以使用name?(例如#{name})
我会让这个例子自言自语:
ruby-1.9.2-p0 > DateTime.now
=> Mon, 14 Feb 2011 20:02:49 +0100
ruby-1.9.2-p0 > User.first.created_at
=> Tue, 04 May 2010 07:03:24 CEST +02:00
ruby-1.9.2-p0 > DateTime.now-User.first.created_at
TypeError: expected numeric or date
from /Users/Jacob/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/date.rb:1356:in `-'
from /Users/Jacob/.rvm/gems/ruby-1.9.2-p0@loyaltric_template/gems/activesupport-3.0.3/lib/active_support/core_ext/date/calculations.rb:98:in `minus_with_duration'
from (irb):47
from /Users/Jacob/.rvm/gems/ruby-1.9.2-p0@loyaltric_template/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
from /Users/Jacob/.rvm/gems/ruby-1.9.2-p0@loyaltric_template/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from /Users/Jacob/.rvm/gems/ruby-1.9.2-p0@loyaltric_template/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
ruby-1.9.2-p0 >
Run Code Online (Sandbox Code Playgroud)
什么?!?
我需要知道模型中当前用户的ID:
def after_save
desc, points=nil, nil
if answer_index == daily_question.correct_answer_index
desc = I18n.t('daily_question.point_log.description.correct')
points=daily_question.points
else
desc = I18n.t('daily_question.point_log.description.incorrect')
end
current_user.give_points(:description => desc,
:points => points
)
end
Run Code Online (Sandbox Code Playgroud)
但我想这不是怎么做的?
问候,
雅各
我想在点击时用微调器替换文本链接.我有这个:
$(document).on('page:fetch', function(e) {
$('.spinner').replaceWith( "<img src='<%= asset_path('layout/spinner.gif') %>'>" );
});
Run Code Online (Sandbox Code Playgroud)
但很明显,与.spinner类的所有链接都获得了微调器.我想只在被点击的链接上替换它,只有它有spinner类.
有什么建议?
我需要一个数组,其中包含当前季度的月份数字.我想供应Date.today然后得到例如.[1,2,3].
我该如何以最简单的方式做到这一点?(不是使用switch/ case).
我有一个模型"SoftwareVersion"的夹具:
testing:
id: 4
version: "4.0"
file_name: MyString4
is_testing: true
Run Code Online (Sandbox Code Playgroud)
并且模型的默认范围:
default_scope where(is_testing: false)
Run Code Online (Sandbox Code Playgroud)
如果我在测试中这样做:
software_versions(:testing)
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
ActiveRecord::RecordNotFound: Couldn't find SoftwareVersion with id=4 [WHERE `software_versions`.`is_testing` = 0]
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?我该如何覆盖呢?
我在这里玩6502汇编程序:http://skilldrick.github.io/easy6502
我做了这个,只是在屏幕上的随机位置放置一个随机颜色像素:
LDY #$00 ; Clear Y
loop:
JSR genPos ; Put new position in memory
LDA $fe ; Get random number for color
STA ($10), Y ; Put pixel on screen
JMP loop
genPos:
STA $10 ; Store accumulator in low
LDA $fe ; Get new random number (WHY, oh why?)
AND #$03 ; Mask out low two bits (=numbers 0-3)
CLC ; Clear carry flag
ADC #2 ; Add 2 (= numbers 2-5)
STA $11 …Run Code Online (Sandbox Code Playgroud) 我正在做一些中间件,它会在它到达Rails之前更改authenticity_token参数.
我可以看到env.inspect同时提供了rack.request.form_vars和rack.request.form_hash.两者都包含真实性令牌.Rails使用哪一个,为什么Rack同时提供?
我在Active Admin 0.5中创建自定义表单.我已经注册了一个页面并通过DSL创建了一个表单:
ActiveAdmin.register_page 'Planning', :namespace => :pos_admin do
content :title => proc{ I18n.t("active_admin.dashboard") } do
form do |f|
f.input :type => :text
f.input :type => :submit
end
end
end
Run Code Online (Sandbox Code Playgroud)
问题是,在提交表单时,我得到一个空的Params哈希.表单标记不包含真实性标记.
我究竟做错了什么?
我不明白这一点:
2.0.0-p598 :003 > a=[1,2,3]
=> [1, 2, 3]
2.0.0-p598 :004 > (a << 1).uniq!
=> [1, 2, 3]
2.0.0-p598 :005 > a
=> [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
我不明白为什么实际上要修改a。我猜想括号会使uniq方法适用于括号中的所有内容,而不是变量。有人可以解释吗?
ruby-1.9.2-p0 > SalesData.last
=> #<SalesData id: 196347, created_at: "2011-04-05 18:53:15", updated_at: "2011-04-05 18:53:15">
ruby-1.9.2-p0 > SalesData.last.created_at
=> Tue, 05 Apr 2011 20:53:21 CEST +02:00
Run Code Online (Sandbox Code Playgroud)
application.rb中:
config.time_zone = 'Copenhagen'
Run Code Online (Sandbox Code Playgroud)
我不明白 - 有人吗?
ruby ×7
6502 ×1
activeadmin ×1
activerecord ×1
arrays ×1
assembly ×1
authlogic ×1
fixtures ×1
jquery ×1
rack ×1
rvm ×1
self ×1
textmate ×1
turbolinks ×1
vim ×1