草庐IT

custom-attribute

全部标签

ruby-on-rails - Rails 4.2 Action Controller :BadRequest custom error message

如果验证失败或参数丢失,我想从我的Controller返回400-错误请求。所以在我的Controller中如果有ifparams["patch"].nil?thenraiseActionController::BadRequest.new("TheJsonbodyneedstobewrappedinsidea\"Patch\"key")end我在我的应用程序Controller中发现了这个错误:rescue_fromActionController::BadRequest,with::bad_requestdefbad_request(exception)renderstatus:4

ruby - self.attribute 与 @attribute 的优势?

假设如下:defcreate_new_saltself.salt=self.object_id.to_s+rand.to_send为什么使用“self”更好。而不是实例变量“@salt”? 最佳答案 如果已定义,使用self.salt=将导致使用salt=方法。这对于确保使用salt=方法内的任何错误检查/转换很有用。然而,这意味着必须有一个明确的salt=方法(例如,可能由attr_accessor创建),这不一定是个好主意。总结:如果你有一个自定义的salt=方法,使用self.salt=;使用@salt=如果你确切地知道你想要

ruby-on-rails - rails : Loading custom class from lib folder in controller

我创建了一个名为lib/services/my_service.rb的文件。#/lib/services/my_service.rbclassMyService...end我想在app/controllers/my_controller中使用它classMyController我收到一个错误消息,指出MyService是一个未初始化的常量。我尝试用导入它require'/lib/services/my_service.rb'但是我得到了cannotloadsuchfile--/lib/services/my_service.rb编辑:我尝试使用application.rb自动加载con

ruby-on-rails - rails 3.2.8 中未定义的 read_inheritable_attribute

升级到rails3.2.8后出现以下错误信息NoMethodError(undefinedmethod`read_inheritable_attribute'forAdminController:Class):谁能解释一下如何解决这个错误?整个轨迹NoMethodError(undefinedmethod`read_inheritable_attribute'forAdminController:Class):vendor/plugins/ssl_requirement/lib/ssl_requirement.rb:45:in`ssl_allowed?'vendor/plugins/s

ruby-on-rails - 使用回形针上传图像时出现 "Unknown attribute: avatar"?

我运行了这个迁移:railsgeneratepaperclipuseravatar它创建了这个迁移文件:classAddAttachmentAvatarToUsers我将其添加到我的编辑用户注册View中:true,class:'form-control'%>当我尝试在编辑用户注册中上传头像时,我收到此错误:ActiveRecord::UnknownAttributeErrorinDevise::RegistrationsController#update未知属性:头像编辑我加了defuser_paramsparams.require(:user).permit(:avatar)end

ruby-on-rails - 如何在 update_attributes 期间以管理员身份跳过验证?

当我尝试以管理员身份编辑用户时,我想跳过验证。型号classUser...attr_accessible:company_id,:first_name,:disabled,as::adminControllerclassAdmin::UsersController...defupdate@user=User.find(params[:id])@user.update_attributes(params[:user],as::admin)redirect_toedit_admin_user_path(@user),:notice=>"UserAccountUpdated"end所以我尝试

ruby-on-rails - Rails 和 attr_accessible : is there a way to raise an exception if a non-mass-assignable attribute is mass-assigned?

如果尝试批量分配attr_accessible不允许的属性,是否有办法让Rails引发错误?这在开发中会很方便,可以提醒我为什么我Shiny的新模型不起作用,也有助于登录生产环境以检测恶意事件。我正在使用Rails2.3.8,但可能很快就会迁移到3。 最佳答案 从Rails3.2开始,这不再需要monkeypatching——rails现在提供了这种行为。将其放入development.rb和test.rb:config.active_record.mass_assignment_sanitizer=:strict

ruby-on-rails - 弃用警告 : Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s)

我将我的Rails5.1.4应用更新到了5.2.0。我的一个模型中有以下范围:scope:by_category,lambda{|category_slug|category_ids=Category.find_by(slug:category_slug)&.subtree_idswhere(category_id:category_ids)}由于该范围,Rails返回以下错误:DEPRECATIONWARNING:Dangerousquerymethod(methodwhoseargumentsareusedasrawSQL)calledwithnon-attributeargume

ruby-on-rails - rails : dynamic columns/attributes on models?

我有一个SaaS应用程序,帐户想要在用户模型上保存不同类型的信息。因此,例如,一个帐户可能想要保存年龄和生日,但在另一个帐户中,他们不会使用这些列,而是想要保存头发颜色和高度的信息。这些只是示例,但我将如何构建我的模型和数据库,以便它可以很好地处理“自定义的、动态的”列,而不会创建太多空属性。 最佳答案 这里有两个选项。1.NoSQL数据库。2.Rails4Store功能。 关于ruby-on-rails-rails:dynamiccolumns/attributesonmodels?,

ruby - Chef 11 : any way to turn attributes into a ruby hash?

我在chef属性中为我的服务生成一个配置。但是,在某些时候,我需要将属性混搭转换为简单的ruby​​散列。这曾经在Chef10中运行良好:node.myapp.config.to_hash但是,从Chef11开始,这不起作用。只有属性的顶层被转换为散列,然后嵌套值仍然是不可变的混搭对象。修改它们会导致如下错误:Chef::Exceptions::ImmutableAttributeModification------------------------------------------------Nodeattributesareread-onlywhenyoudonotspecif