<%= f.check_box :openid_enabled %>
<%= f.label :openid_enabled, 'OpenID' %>
Run Code Online (Sandbox Code Playgroud)
上面的代码生成了这个HTML
<input type="hidden" value="0" name="application[openid_enabled]">
<input type="checkbox" value="1" name="application[openid_enabled]" id="application_openid_enabled">
<label for="application_openid_enabled">OpenID</label>
Run Code Online (Sandbox Code Playgroud)
并且标签显示为
[x]
OpenID
Run Code Online (Sandbox Code Playgroud)
代替
[x] OpenID
Run Code Online (Sandbox Code Playgroud)
我是否需要设计样式或者rails帮助程序是否具有一些内置功能?
添加
我在我的Rails应用程序中使用twitter bootstrap CSS框架.
我正在阅读Rails 3 Way的书,并感到困惑:
:after_add => callback 通过<<方法将记录添加到集合中后调用。不是由集合的create方法触发的
据我了解book.chapters.create(title:'第一章')不会调用before_add回调,但实际上是在调用。
class Book < ActiveRecord::Base
attr_accessible :title
has_many :chapters, :before_add => :add_chapter
private
def add_chapter(chapter)
logger.error('chapter added to book')
end
end
class Chapter < ActiveRecord::Base
belongs_to :book
attr_accessible :title
end
Run Code Online (Sandbox Code Playgroud)
在控制台中(最小)
> b = Book.first
Book Load (0.1ms) SELECT "books".* FROM "books" LIMIT 1
> b.chapters.create(title: 'Last Chapter')
begin transaction
chapter added to book
INSERT INTO "chapters" ....
commit transaction
Run Code Online (Sandbox Code Playgroud)
在这里,您可以看到after_add为调用了回调create。
我误会了吗?
编辑
b.chapters.new(title: 'New Chapter')
b.chapters.build(title: 'New Chapter') …Run Code Online (Sandbox Code Playgroud) 我在eclipse indigo的jsp中添加以下EL时遇到错误.
<form action="${pageContext.request.contextPath}/user">
...
</form>
Run Code Online (Sandbox Code Playgroud)
但是,应用程序运行正常,没有任何编译错误.我仔细检查了servlet-api 2.5和jst 1.2 jar是否在Eclipse IDE的类路径中.
如果我删除${pageContext.request.contextPath}它不会显示任何错误.
任何人都可以帮我解决这个错误吗?
使用TweetStream运行以下示例时,我得到提到的错误.
tweets.rb
require 'tweetstream'
TweetStream.configure do |config|
config.consumer_key = '<CONSUMER KEY>'
config.consumer_secret = '<CONSUMER SECRET>'
config.oauth_token = '<OAUTH TOKEN>'
config.oauth_token_secret = '<OAUTH TOKEN SECRET'
config.auth_method = :oauth
end
TweetStream::Client.new.track('ruby') do |status|
puts "#{status.text}"
end
Run Code Online (Sandbox Code Playgroud)
错误
$ ruby tweets.rb
/home/amit/.rvm/gems/ruby-1.9.3-p194/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:96:in `track': undefined method `extract_options!' for ["ruby"]:Array (NoMethodError)
from tweets.rb:11:in `<main>'
https://github.com/intridea/tweetstream
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我已经使用Capistrano 3部署了应用程序.我一直在跟踪错误.
`require': cannot load such file -- bundler/setup (LoadError)
Run Code Online (Sandbox Code Playgroud)
这是cron选项卡列表
PATH=/home/deploy/magnificent/shared/bundle/ruby/2.2.0/bin:/usr/local/rvm/gems/ruby-2.2.2/bin:/usr/local/rvm/gems/ruby-2.2.2@global/bin:/usr/local/rvm/rubies/ruby-2.2.2/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
* * * * * /bin/bash -l -c 'cd /home/deploy/magnificent/releases/20150830045359 && bin/rails runner -e production '\''Document.process_pending'\'' >> log/cron_standard.log 2>> log/cron_error.log'
Run Code Online (Sandbox Code Playgroud)
和schedule.rb
env :PATH, ENV['PATH']
set :output, { error: 'log/cron_error.log', standard: 'log/cron_standard.log'}
every 1.minutes do
runner 'Document.process_pending'
end
Run Code Online (Sandbox Code Playgroud)
请注意,所有宝石都安装在defaultgemset中
我有一个字符串表示带有时区的日期/时间.我想将时区部分更改为UTC+00:00
请帮我写REGEXT匹配+05:30,-03:30等等而代之以+00:00
我试过 "2012-04-17T15:40+05:30".gsub!(/\+\d\d:\d\d/, '+00:00')给了我预期的结果,但我不知道如何匹配-5:30
我很感激,如果有人帮我写了正则表达式,并且可以使用2012-04-17T15:40+05:30和2012-04-17T15:40-05:30
谢谢,阿米特帕特尔
这是装饰师
应用程序/装饰/ campaign_decorator.rb
class CampaignDecorator < Draper::Decorator
delegate_all Campaign::Campaign
def created_at
helpers.content_tag :span, class: 'time' do
object.created_at.strftime("%a %m/%d/%y")
end
end
def custom_method
'hello there!'
end
end
Run Code Online (Sandbox Code Playgroud)
当我调用CampaignDecorator.custom_method它时,找不到方法.还CampaignDecorator.first.created_at返回未格式化的日期.
任何人都可以告诉我我错过了什么?
我有许多服务类,其call方法在参数上有变化。
notify我想在每个方法的末尾调用一个函数call。我不想修改这些服务类,但我愿意修改基类。
我正在玩,ActiveSupport::Callbacks但它不能达到不修改服务类的目的。
require 'active_support'
class Base
include ActiveSupport::Callbacks
define_callbacks :notifier
set_callback :notifier, :after do |object|
notify()
end
def notify
puts "notified successfully"
end
end
class NewPost < Base
def call
puts "Creating new post on WordPress"
# run_callbacks :notifier do
# puts "notifying....."
# end
end
end
class EditPost < Base
def call
puts "Editing the post on WordPress"
# run_callbacks :notifier do
# puts "notified successfully"
# end
end
end
person …Run Code Online (Sandbox Code Playgroud) 使用 RSpec 的模拟/存根,如何为该find_by_id方法编写单元测试?
我想使用 RSpec,而不是 WebMock 或 VCR。如何为请求/响应创建存根?
class RapidApiClient
HOST_URL = 'https://brianiswu-open-brewery-db-v1.p.rapidapi.com/breweries'
API_KEY = 'private_api_key'
def request_api(url)
Excon.get(
url,
headers: {
'X-RapidAPI-Host' => HOST_URL,
'X-RapidAPI-Key' => 'API_KEY'
}
)
end
def find_by_id(id)
response = request_api("#{HOST_URL}/#{id}")
return nil if response.status != 200
JSON.parse(response.body)
end
end
Run Code Online (Sandbox Code Playgroud)
响应是:
[
{"id":4 , "name":"Ban Brewing Company" , "brewery_type":"micro", "city":"Tulsa" , "state":"OK"}
{"id":44,"name":"Tab Brewing" "brewery_type":"micro", "city":"Birmingham", "state":"MO"}
]
Run Code Online (Sandbox Code Playgroud) 我是Rails的新手,并试图用TDD和BDD编写应用程序.
现在,在其中一个模型中,对字段的长度进行了验证.有一个RSpec有一个例子来检查这个特定字段的长度验证.
这是Model类
class Section < ActiveRecord::Base
# Validations
validates_presence_of :name, length: { maximum: 50 }
end
Run Code Online (Sandbox Code Playgroud)
和RSpec
require 'spec_helper'
describe Section do
before do
@section = Section.new(name:'Test')
end
subject { @section }
# Check for attribute accessor methods
it { should respond_to(:name) }
# Sanity check, verifying that the @section object is initially valid
it { should be_valid }
describe "when name is not present" do
before { @section.name = "" }
it { should_not be_valid }
end
describe …Run Code Online (Sandbox Code Playgroud) 在部署Rails 3应用程序时,我们遇到以下错误.请帮助解决此问题.
$ cap deploy
triggering load callbacks
* 2015-03-17 19:25:22 executing `deploy'
* 2015-03-17 19:25:22 executing `deploy:update'
** transaction: start
* 2015-03-17 19:25:22 executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "git ls-remote git@github.com:BoTreeConsultingTeam/IntegrityMatters.git master"
command finished in 3579ms
* executing "if [ -d /home/ubuntu/integrity_matters/shared/cached-copy ]; then cd /home/ubuntu/integrity_matters/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard eb49600f7de21f4b2b1de8884f70dd5a88837f41 && git clean -q -d -x -f; else git clone …Run Code Online (Sandbox Code Playgroud) ruby ×5
activerecord ×3
amazon-ec2 ×1
callback ×1
capistrano ×1
capistrano3 ×1
crontab ×1
css ×1
draper ×1
eclipse ×1
jsp ×1
jstl ×1
regex ×1
rspec ×1
tweetstream ×1
view-helpers ×1
whenever ×1