我有一个用户注册表单,其中包含常用字段(名称,电子邮件,密码等)以及"team_invite_code"字段和"角色"弹出菜单.
在创建用户之前 - 仅在用户角色是"孩子"的情况下 - 我需要:
如何在Rails 2.3.6中编写适当的验证?
我尝试了以下内容,但它给了我错误:
validate :child_and_team_code_exists
def child_and_team_code_exists
errors.add(:team_code, t("user_form.team_code_not_present")) unless
self.is_child? && Team.scoped_by_code("params[:team_code]").exists?
end
>> NameError: undefined local variable or method `child_and_team_code_exists' for #<Class:0x102ca7fa8>
Run Code Online (Sandbox Code Playgroud)
更新: 此验证代码有效:
def validate
errors.add_to_base(t("user_form.team_code_not_present")) if (self.is_child? && !Team.scoped_by_code("params[:team_code]").exists?)
end
Run Code Online (Sandbox Code Playgroud) 在Rails 3.2.11应用程序中,我正在尝试将我的应用程序发布到Heroku.
在assets文件夹中,我有一个pdf子文件夹,里面有一些pdf文件.
在我的production.rb文件中,我添加了以下内容:
config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif *.pdf]
config.assets.precompile += ["*.js"]
config.assets.precompile += ["*.css"]
config.assets.precompile += ['pdf/*']
config.assets.precompile += %w( ricerca_wg.pdf )
Run Code Online (Sandbox Code Playgroud)
如果我检查控制台上的pdf资产路径,我会得到:
Rails.application.config.assets.paths
# [
# "/Users/Augusto/Sites/wisegrowth/app/assets/images",
# "/Users/Augusto/Sites/wisegrowth/app/assets/javascripts",
# "/Users/Augusto/Sites/wisegrowth/app/assets/pdf",
# "/Users/Augusto/Sites/wisegrowth/app/assets/stylesheets",
# "/Users/Augusto/Sites/wisegrowth/vendor/assets/javascripts",
# "/Users/Augusto/Sites/wisegrowth/vendor/assets/stylesheets",
# "/Users/Augusto/.rvm/gems/ruby-1.9.3-p551/gems/jquery-rails-2.3.0/vendor/assets/javascripts",
# "/Users/Augusto/.rvm/gems/ruby-1.9.3-p551/gems/coffee-rails-3.2.2/lib/assets/javascripts",
# "/Users/Augusto/.rvm/gems/ruby-1.9.3-p551/gems/formtastic-2.1.1/app/assets/stylesheets"
# ]
Run Code Online (Sandbox Code Playgroud)
但是当我跑步的时候
rake assets:precompile RAILS_ENV=production
Run Code Online (Sandbox Code Playgroud)
一切都是预编译但是pdf文件和我在Heroku上的生产应用程序中我收到以下错误:
ActionView::Template::Error (ricerca_wg.pdf isn't precompiled):
Run Code Online (Sandbox Code Playgroud) 我正在尝试向通过User模型创建的对象添加新属性.
a = User.find(7)
Run Code Online (Sandbox Code Playgroud)
我明白了:
#<User id: 7, name: "Ewah", email: "stugnuz@yahoo.it", password_salt: "t4vPG0hgQVQMyIFeri6", persistence_token: "98f0cdfcb9462f3f74b1ae6ef48f0d72a015c6e04adda95fcd0...", login_count: 387, failed_login_count: 0, current_login_at: "2011-03-15 08:03:54", last_login_at: "2011-03-14 23:17:45", current_login_ip: "93.34.49.190", last_login_ip: "93.34.38.47", score: 2726, level: 7, created_at: "2010-10-13 22:33:21", updated_at: "2011-03-28 13:28:40", admin: true, avatar_file_name: "11.jpg", avatar_file_size: 78456, avatar_content_type: "image/jpeg", perishable_token: "PXTFtiMBxcoH3Iv7TmRh", score_this_week: 0, get_news: true, get_notices: true, bio: "Ho perso per strada Minnie anche in onore di Ken Sa...", invitation_id: nil, can_approve: false, no_score: false, facebook_uid: "1052260327", facebook_session_key: "", crypted_password: "05e5080268598607f5d1ece82db1b4d4c6ae12deeb880896ad2...", …Run Code Online (Sandbox Code Playgroud) 我正在运行Spree 1.3.1,我正在尝试自定义Taxon show页面.
我希望它返回当前Taxon中包含的产品,最终通过属性或选项值进行过滤.
例如,假设我正在看内衣系列的Taxon.我想过滤所显示的产品,提供一定的尺寸(option_type).在这种情况下,我应该只列出具有所需大小的变体的产品.
我还希望能够通过"fit"属性过滤产品.通过滑动过滤,我应该只能列出当前分类中具有所需属性的产品.
这是Taxon控制器显示动作:
Spree::TaxonsController.class_eval do
def show
@taxon = Spree::Taxon.find_by_permalink!(params[:id])
return unless @taxon
@searcher = Spree::Config.searcher_class.new(params)
@searcher.current_user = try_spree_current_user
@searcher.current_currency = current_currency
@products = @searcher.retrieve_products
respond_with(@taxon)
end
end
Run Code Online (Sandbox Code Playgroud)
我该如何修改它以满足我的需求?
在我的Rails 3.2.2应用程序中我正在尝试使用i18n,但有些东西无法正常工作.
事实上,"t"方法不起作用,只有"i18n.t"有效.
所以,例如:
t(:login)
=> login
Run Code Online (Sandbox Code Playgroud)
代替:
i18n.t(:login)
=> Provide the necessary login info
Run Code Online (Sandbox Code Playgroud)
你能帮我搞清楚我做错了什么吗?
谢谢,奥古斯托
UPDATE
我使用pry来显示t帮助器的来源并得到了这个:
来自:/Users/phishman/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.2/lib/action_view/helpers/translation_helper.rb @第46行:行数:16所有者:ActionView: :助手:: TranslationHelper可见性:公众
def translate(key, options = {})
options.merge!(:rescue_format => :html) unless options.key?(:rescue_format)
if html_safe_translation_key?(key)
html_safe_options = options.dup
options.except(*I18n::RESERVED_KEYS).each do |name, value|
unless name == :count && value.is_a?(Numeric)
html_safe_options[name] = ERB::Util.html_escape(value.to_s)
end
end
translation = I18n.translate(scope_key_by_partial(key), html_safe_options)
translation.respond_to?(:html_safe) ? translation.html_safe : translation
else
I18n.translate(scope_key_by_partial(key), options)
end
end
3] pry(main)> show-source helper.t
From: /Users/phishman/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.2/lib/action_view/helpers/translation_helper.rb @ line 46:
Number of lines: 16 …Run Code Online (Sandbox Code Playgroud) 我在Rails 3.2.2中,我有一个Variant类:
class Variant < ActiveRecord::Base
has_one :twin_variant
end
Run Code Online (Sandbox Code Playgroud)
我希望能够将这个类中的两个对象关联成为"双胞胎".我想创建:
v1 = Variant.new
v1.name = "Fantastic variant"
v1.save
Run Code Online (Sandbox Code Playgroud)
然后我想有一个创建双变量的方法:
v2 = Variant.new
v2.name = "Fantastic variant twin"
v2.save
v1.twin_variant = v2
Run Code Online (Sandbox Code Playgroud)
然后这两个对象应该相互关联,这样:
v1.twin_variant
=> v2
v2.twin_variant
=> v1
Run Code Online (Sandbox Code Playgroud)
这可能吗?我该如何建立关联?
我按照Shopify说明获取特定应用/商店组合的永久令牌(http://api.shopify.com/authentication.html).
我能够获取临时令牌,然后使用简单的html表单来接收永久令牌:
但我得到的回应是:{"error":"invalid_request"}
你能帮我吗?我到处搜索(Stackoverflow,Shopify支持论坛等等),但找不到如何解决这个问题的线索.我的应用程序在线并在Heroku上托管.
谢谢,

今天,我创建了一个工作应用程序的副本,它在Heroku上完美运行,并尝试将其部署在Heroku上作为新项目的起点.
我将新文件夹添加为git存储库,在GitHub上创建了一个新的远程存储库,编辑了配置文件并为数据库提供了新名称,创建了新数据库并尝试在Heroku上部署.
现在应用程序在启动时崩溃,因为Heroku在我的源文件中发现了一些utf-8文本并且无法识别它们:
2011-06-27T14:23:10+00:00 app[web.1]: /app/app/controllers/home_controller.rb:118: invalid multibyte char (US-ASCII)
2011-06-27T14:23:10+00:00 app[web.1]: /app/app/controllers/home_controller.rb:118: syntax error, unexpected $end, expecting '}'
2011-06-27T14:23:10+00:00 app[web.1]: ...tue azioni, conquista la città!"}
Run Code Online (Sandbox Code Playgroud)
如何告诉Rails和Heroku我的所有源文件都是utf-8编码的?我应该在每个文件中添加UTF-8 BOM吗?这很疯狂,我在之前的应用程序中没有这样做,效果很好.
我正在使用Rails 2.3.6.
您好,我正在 Shopify 应用中使用 Shopify gem,我正在寻找有关如何处理与 Shopify 的 API 连接的建议。
我正在使用 webhooks 和 delay_jobs,所以我需要一种方法来打开控制器外部的连接。
目前我将此方法添加到我的 Shop 模型中:
def connect_to_store
session = ShopifyAPI::Session.new(self.url, self.access_token)
session.valid?
ShopifyAPI::Base.activate_session(session)
end
Run Code Online (Sandbox Code Playgroud)
所以我可以很容易地打开连接,例如:
Shop.find(1).connect_to_store
ShopifyAPI::Shop.current.name
Run Code Online (Sandbox Code Playgroud)
问题是,在我的 Product 模块中,我需要在几个方法中打开连接,但我最终多次调用 connect_to_store 方法,我担心在没有真正需要的情况下打开到同一个商店的多个连接。
有没有办法检查一个连接是否已经打开,只有在没有找到另一个连接时才打开一个新连接?
谢谢,奥古斯托
- - - - - - - - - - 更新 - - - - - - - - - -
我更好地解释了我的问题。
假设在我的产品模型中,我想查看给定产品的 compare_at_price 是否大于其价格,在这种情况下,我想向 Shopify 产品添加“促销”标签。
在我的产品模型中,我有:
class Product < ActiveRecord::Base
belongs_to :shop
def get_from_shopify
self.shop.connect_to_store
@shopify_p = ShopifyAPI::Product.find(self.shopify_id)
end
def add_tag(tag)
@shopify_p = …Run Code Online (Sandbox Code Playgroud) 9.3我有一种奇怪的行为,我无法理解为什么:
s = self.shopify_p
s.title
=> "Disco (Wholesale)"
Run Code Online (Sandbox Code Playgroud)
现在我想要一个新的变量,其中s.title的内容没有"(批发)"部分.所以我做了以下事情:
original_title = s.title
=> "Disco (Wholesale)"
original_title[" (Wholesale)"] = ""
=> ""
Run Code Online (Sandbox Code Playgroud)
如果我这样做:
original_title
=> "Disco"
Run Code Online (Sandbox Code Playgroud)
哪个是好的,但奇怪的是,似乎最后一个字符串替换甚至影响了原始的s变量:
s.title
=> "Disco"
Run Code Online (Sandbox Code Playgroud)
我真的无法理解这个......你能告诉我这里发生了什么吗?
s.title应该仍然是"迪斯科(批发)"......或不?
ruby ×2
shopify ×2
access-token ×1
associations ×1
heroku ×1
oauth ×1
spree ×1
string ×1
validation ×1