比方说我有:
@string = "it is a <a href="#">string</a>"
Run Code Online (Sandbox Code Playgroud)
我想以两种方式在我的应用程序的不同部分使用它:
第一个可以使用html_safe:
@string.html_safe
Run Code Online (Sandbox Code Playgroud)
这是一个字符串
我怎样才能实现第二个?
这是一个字符串.
我使用SQLite3进行开发,使用PostgreSQL进行部署.但是,我面临以下问题:
我的简单搜索使用SQLite3:
def self.search(search)
if search
find(:all, :conditions => ["style LIKE ? OR construction LIKE ?", "%#{search}%", "%#{search}%"])
else
find(:all)
end
end
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用PostgreSQL,我需要替换LIKEfor ILIKE来解决问题:
def self.search(search)
if search
find(:all, :conditions => ["style ILIKE ? OR construction ILIKE ?", "%#{search}%", "%#{search}%"])
else
find(:all)
end
end
Run Code Online (Sandbox Code Playgroud)
是否有"Ruby方式"在任何数据库中进行这些搜索?
编辑 - 基于您的答案我不相信我会找到一个通用的Ruby解决方案.
我也跟着Ruby on Rails的教程:学习的榜样导轨-由迈克尔·哈特尔,在最终的Gemfile显示这两个数据库...嗯,令人失望...
我有以下自我指涉关联:
class Action < ActiveRecord::Base
# self referential association
has_many :action_parents
has_many :parents, through: :action_parents
has_many :action_children, class_name: 'ActionParent', foreign_key: 'parent_id'
has_many :children, through: :action_children, source: :action
…
def should_finish
should_start + duration
end
def should_start
# my_start is a field in db: if there are no parents (root) it will use this field
return my_start if parents.empty?
parents.map(&:should_finish).sort.last
end
end
Run Code Online (Sandbox Code Playgroud)
我的问题是这样的事实,should_finish并且should_start正在相互呼叫,即使我预先加载父母,它也会导致许多查询:
Action.includes(:parents).last.should_finish
# a new query every time it checks for parents
Run Code Online (Sandbox Code Playgroud)
如何高速缓存的任何想法actions和 …
Auser有很多comments,所以我想有一个user与它comment相关联的工厂(user_with_comment):
factory :user, class: User do |t|
...
factory :user_with_comment do |t|
after(:create) do |user|
FactoryGirl.create(:comment, user_id: user.id)
end
end
Run Code Online (Sandbox Code Playgroud)
它正常工作......当我打电话FactoryGirl.create(:user_with_comment),它会创建user和有关comment我的test db。
但是,我在以下方面面临一些问题controller_spec:
使用let我必须重新加载user才能看到comment:
let(:user) { FactoryGirl.create(:user_with_comment) }
user.comments.size #=>0
user.reload
user.comments.size #=>1
Run Code Online (Sandbox Code Playgroud)
一种解决方案是使用before(:each),但它会在每次测试之前创建venda和comment:
before(:each) do
@user = FactoryGirl.create(:user_with_comment)
end
@user.comments.size #=>1
Run Code Online (Sandbox Code Playgroud)
或者,我可以user在每次测试之前重新加载,但它也会命中数据库:
let(:user) { FactoryGirl.create(:user_with_comment) …Run Code Online (Sandbox Code Playgroud) 将我的Xcode升级到4.6后,Kobold2d 2.0.4会产生如下错误.有人也遇到过这个问题吗?
Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang failed with exit code 1
CompileC /Users/ernnug/Library/Developer/Xcode/DerivedData/Kobold2D-brplvdgzzqjavebyfesirygztebf/Build/Intermediates/Kobold2D-Libraries.build/Debug-iphoneos/kobold2d-ios.build/Objects-normal/armv7/CCAnimationExtensions.o kobold2d/cocos2d-ext/CCAnimationExtensions.m normal armv7 objective-c com.apple.compilers.llvm.clang.1_0.compiler
cd /Users/ernnug/Kobold2D/Kobold2D-2.0.4/__Kobold2D__
setenv LANG en_US.US-ASCII
setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x objective-c -arch armv7 -fmessage-length=0 -std=gnu99 -Wno-trigraphs -fpascal-strings -O0 -Werror -Wmissing-field-initializers -Wno-missing-prototypes -Wreturn-type -Wno-implicit-atomic-properties -Wno-receiver-is-weak -Wformat -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-enum-conversion -Wsign-compare -Wno-shorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wstrict-selector-match -Wundeclared-selector -Wno-deprecated-implementations -DDEBUG -DCOCOS2D_DEBUG=1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk -ffast-math -fstrict-aliasing -Wprotocol -Wdeprecated-declarations -g -fvisibility=hidden -Wno-sign-conversion -miphoneos-version-min=4.3 -I/Users/ernnug/Library/Developer/Xcode/DerivedData/Kobold2D-brplvdgzzqjavebyfesirygztebf/Build/Intermediates/Kobold2D-Libraries.build/Debug-iphoneos/kobold2d-ios.build/kobold2d-ios.hmap -I/Users/ernnug/Kobold2D/Kobold2D-2.0.4/__Kobold2D__/libs/cocos2d-iphone/cocos2d …Run Code Online (Sandbox Code Playgroud) 我有以下协会:
class Venue < ActiveRecord::Base
has_many :sales
end
class Sale < ActiveRecord::Base
has_many :sale_lines
has_many :beverages, through: :sale_lines
end
class SaleLine < ActiveRecord::Base
belongs_to :sale
belongs_to :beverage
end
class Beverage < ActiveRecord::Base
has_many :sale_lines
has_many :sales, through: :sale_lines
has_many :recipes
has_many :products, through: :recipes
end
class Recipe < ActiveRecord::Base
belongs_to :beverage
belongs_to :product
end
class Product < ActiveRecord::Base
has_many :recipes
has_many :beverages, through: :recipes
end
Run Code Online (Sandbox Code Playgroud)

我wan't看到各场馆销售的产品数量,所以基本上我要乘以recipe.quantity由sale_line.quantity一个特定的产品.
我想打电话@venue.calc_sales(product)来获得售出的数量product.
在课堂上Venue我试图通过以下方式计算它:
class Venue …Run Code Online (Sandbox Code Playgroud) ruby activerecord ruby-on-rails ruby-on-rails-3 ruby-on-rails-4
<div class="row">
<div class="large-12 columns">
<ul class="button-group ">
<li><a class="tiny secondary button" href="#">3 months</a></li>
<li><a class="tiny secondary button" href="#">6 months</a></li>
<li><a class="tiny secondary button" href="#">9 months</a></li>
<li><a class="tiny secondary button" href="#">1 year</a></li>
<li><a class="tiny secondary button" href="#">2 years</a></li>
</ul>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我试图添加text-center到列,但ul也large-centered没有成功.
我相信有一个班级foundation可以使按钮组居中,但我找不到它.
我收到以下错误:
Capybara::ElementNotFound: Unable to find field "username"
./spec/controllers/sessions_controller_spec.rb:10:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
规格:
require 'spec_helper'
describe SessionsController do
before :each do
@user = FactoryGirl.create(:user)
end
context 'creating a new session' do
it 'can set the current_user variable to the logged user' do
visit '/login'
fill_in 'username', with: 'gabrielhilal' #I have tried `Username` as well
fill_in 'password', with: 'secret'
click_button 'Login'
current_user.should == @user
current_user.username.should == 'gabrielhilal'
end
end
context 'destroying existing session' do
xit 'can destroy the current_user' …Run Code Online (Sandbox Code Playgroud) 我在用Rails的url帮手,而不是路径佣工(在某些情况下),因为我与使用子域名的应用程序工作,所以我不得不将域名选项作为参数.
但是这会导致链接呈现为:
http://sub.domain.dev/the-page?locale=en
我尝试在应用程序控制器中使用以下变体,但无济于事:
def default_url_options(options={})
{ :locale => :en }
end
Run Code Online (Sandbox Code Playgroud)
如何删除该区域设置参数?
我正在使用RefineryCMS.
subdomain locale ruby-on-rails refinerycms ruby-on-rails-3.2
我正在使用搜索表单,并希望在rails表单上的ruby中使用GET请求.我用的是这样的东西
1)在视野中
form_tag(:action => "actionxyz", :method => "get")
Run Code Online (Sandbox Code Playgroud)
2)在路线中
get 'actionxyz', :controller => :controllerabc
Run Code Online (Sandbox Code Playgroud)
检查请求时,可以看到以下内容
<form accept-charset="UTF-8" action="/actionxyz?method=get" method="post"><div style="margin:0;padding:0;display:inline">
Run Code Online (Sandbox Code Playgroud)
我在网址中看到的更多,/actionxyz?method=get而不是我提供的搜索字符串.提供的搜索字符串显示在POST数据中.这是否意味着rails没有使用GET方法,或者我的解释错了.请澄清 ..