Tag: ruby

Actionmailer中的电子邮件NoMethodError

我是购买确认的NoMethodError。 不知道我在这里缺少什么。 NoMethodError在/购买未定义的方法`email’为nil:NilClass purchase_confirmationapp/mailers/purchase_mailer.rb # en.purchase_mailer.purchase_confirmation.subject 8 # 9 def purchase_confirmation(purchase) 10 @greeting = “Hi” 11 12 mail to: purchase.email, subject: “Purchase Confirmation” 13 end 14 end createapp/controllers/purchases_controller.rb 1 class PurchasesController [:index, :edit, :update, :destroy] 3 def create 4 @purchase = Purchase.new(params[:purchase]) 5 if @purchase.save 6 PurchaseMailer.purchase_confirmation(@purchase).deliver 7 redirect_to “/thankyou” 8 else 9 10 render […]

Rails 3:如果我将内容放在部分内容中,则为“未定义的局部变量或方法”

我有下表 Photo Ratings Average Rating Your Rating 如果我在视图中有它可以正常工作,但是当我把它放在部分_rating.html.erb我得到了 undefined local variable or method ‘msg’ for #<#:0x003463d9f388> 我链接到部分通过 “rating” %> ,我必须添加到部分链接,以便我不会得到错误? 谢谢

如何在Rails 3.2.3中本地化一般错误消息?

我使用以下部分显示Rails 3.2.3中我的大多数模型的错误消息: # _error_messages.html.erb prohibited this from being saved: There were problems with the following fields: 这很有效,直到我决定使用I18n本地化我的应用程序。 我为德语内容创建了一个新文件de.yml ,其中包含了这个(以及其他许多内容): # de.yml errors: &errors format: ! ‘%{attribute} %{message}’ messages: blank: muss ausgefüllt werden template: body: ! ‘Bitte überprüfen Sie die folgenden Felder:’ header: one: ! ‘Konnte %{model} nicht speichern: ein Fehler.’ other: ! ‘Konnte %{model} nicht […]

如何更新AR模型上属性的属性?

我正在使用piggybakgem 。 我有一个产品q : 1.9.3p194 :009 > q => #<Product id: 20, name: "iPad", description: "\r\n\tiPad 11" tablet that will change the way…”, price: 499.0, vendor_id: 11, created_at: “2012-12-15 23:40:27”, updated_at: “2013-01-01 00:18:39”, image: “ipad.png”, sku: “AB-09123”> q有一个属性piggybak_sellable ,它有其他属性。 1.9.3p194 :010 > q.piggybak_sellable => nil 这是在Rails管理UI中分配的Product可用属性的示例。 1.9.3p194 :011 > p.piggybak_sellable => #<Piggybak::Sellable id: 1, sku: […]

Rails :: ActiveRecord在深拷贝副本中保留auto_increment id

我需要创建一个包含要操作的模型对象的临时数组。 我需要auto_increment主键,但我不希望保持关联,因为当我从数组中删除它时,它也将它从数据库中删除(感谢rails,非常不方便)。 我使用object.dup创建的深层副本的id标记为nil。 如何将它们保存在副本中?

用RubyInline声明类方法

我的test_func.rb有以下几行代码 require ‘inline’ class TestFunc inline do |builder| builder.c ‘ int testfunc() { return 0; }’ end end 我只能从一个对象而不是类调用该函数。 我可以称之为, obj = TestFunc.new obj.testfunc() 但是我应该怎么声明我可以打电话给, TestFunc.testfunc()

如何在Rails中访问嵌套参数

在Controller中,我正在尝试访问深度嵌套的参数。 这是我的参数跟踪。 Parameters: {“utf8″=>”✓”, “authenticity_token”=>”2j+Nh5C7jPkNOsQnWoA0wtG/vWxLMpyKt6aiC2UmxgY=”, “inventory”=>{“ingredients_attributes”=>{“0″=>{“ingredient_name”=>”Bread”}}, “purchase_date”=>”11”, “purchase_price”=>”11”, “quantity_bought”=>”11”}, “commit”=>”Create Inventory”} 我正试图从中检索“面包”。 我尝试了params[:inventory][:ingredient][:ingredient_name]和其他变种。 什么是正确的styntax? 如果重要, Inventory has_many :ingredients Inventory accepts_nested_attributes_for :inventories 谢谢!

Rails 5:i18n用于命名空间模型的枚举

在命名空间模型中,我对i18n有点困惑。 目前我在模型/app/media/medias.rb中有这样的行: enum territory: { local: 1, global: 2 } 在模态窗口/views/media/medias/_newmedia.html.erb我有这个: 在/config/locales/models/medias.en.yml我有这个: en: media: medias: territories: local: Local_EN global: Global_EN 在控制台中我看到查询完成,没有抛出错误,但我的视图没有出现。 当我更改为简单的Media::Medias.territories.keys我在视图中的下拉列表中显示了键值。 我该如何解决这个问题? 谢谢! 到目前为止,我已经尝试从这个答案中实现代码 。 在视图中它很好地提供了翻译,但是在创建操作时我收到错误,因为它试图保存不是枚举键,而是本地化值。 解决方案 好吧,看来这个答案在我的案例中是解决方案 。 如果有人需要,我的示例应该如何使用命名空间模型: 1)在模型/app/media/medias.rb中 enum territory: { local: 1, global: 2 } def self.territory_attributes_for_select territories.map do |territory, _| [I18n.t(“activerecord.attributes.#{model_name.i18n_key}.territories.#{territory}”), territory] end end 2)然后在视图中部分/views/media/medias/_newmedia.html.erb 3)最后在/config/locales/en.yml en: activerecord: attributes: […]

Rails 3 – 帮助使用电子邮件validation正则表达式

提供以下REGEX如果提供的电子邮件域与INVALID_EMAILs不匹配似乎无限期运行并锁定rails服务器(我的本地环境)。 INVALID_EMAILS = %w(gmail.com googlemail.com yahoo.com ymail.com rocketmail.com hotmail.com facebook.com) def valid_email_domain(emailAddy) reg = Regexp.new /#{User::INVALID_EMAILS.map{|a| Regexp.quote(a)}.join(“|”)}/ if emailAddy.scan(reg).size == 0 return true else return false end end 那里有可以提供反馈的正则表达专家吗? 谢谢 更新: 控制器: def create @user = User.new # User EmailVeracity to validate the email address email = EmailVeracity::Address.new(params[:user][:email]) Rails.logger.info ‘Email Check Result’ Rails.logger.info valid_email_domain(params[:user][:email]) Rails.logger.info […]

切换到mysql数据库时rake db:load命令中的问题

我正在研究Rails 4中的一个项目,之前我使用sqlite3作为数据库,我切换到mysql数据库并更改了database.yml文件中的所有配置。我在运行rake db:migrate时也成功完成了所有迁移。 i was succesfull in ruinning rake db:dump sudo gem install mysql rake db:create the problem i am facing is while running rake db:schema:load i am getting the following error while running the above command ActiveRecord::StatementInvalid: Mysql2::Error: Cannot delete or update a parent row: a foreign key constraint fails: DROP TABLE `advance_salaries` CASCADE […]