jjzjj

attribute

全部标签

ruby-on-rails - 错误 : When assigning attributes, 您必须将散列作为参数传递

嗨,我刚开始使用ruby​​,我正在编写Controller和Controller规范,但我遇到了一些问题。文档.rbclassDocument文档Controller.rbclassAPI::DocumentsControllerdocuments_controller_spec.rbdescribe"POST'index'"dobefore{@attr=FactoryGirl.attributes_for(:document)}describe"failure"dodescribe"withmissingparameters"dobefore{@attr.each{|key,val

ruby-on-rails - Rails,activerecord : self[:attribute] vs self. 属性

在Rails中访问事件记录列/属性时,使用self[:attribute]与self.attribute有什么区别?这会影响getter和setter吗? 最佳答案 它们都只是获取属性的方法-它们都只是getter。self.attribtue是一个更“传统”的getter,而self[:attribute]基本上只是[]方法。在使用两者之间切换不会产生任何影响。我建议只使用self.attribute方法,因为它在语法上更好。但是,当其他内容覆盖self.attribute方法时,使用self[:attribute]会派上用场。例

ruby-on-rails - Elasticsearch /轮胎 : How do I filter a boolean attribute?

我想过滤我类(class)的私有(private)bool值,以便它只显示非私有(private)但对我不起作用的资源。(我大大简化了代码)mappingdoindexes:private,type:"boolean"indexes:name,type:"string"endenddefself.search(params)tire.search(load:true,page:params[:page],per_page:20)doquery{stringparams[:query]}ifparams[:query].present?#SofarI'vetried...#filter:

ruby-on-rails - 尝试使用 accepts_nested_attributes_for 和 has_and_belongs_to_many 但未填充连接表

我正在学习RoR并尝试使用accepts_nested_attributes_for和has_and_belongs_to_many来提交传统上为两种形式的信息。我在一些网站上看到它们兼容,一些网站不兼容,还有一些网站不知道。作为引用,我使用的是Rails2.3.4。我尝试根据nestedmodels上的Ryan'sScraps教程对我的解决方案进行建模从我尝试调试的内容来看,我似乎有两个问题,但我不确定为什么。当我提交包含嵌套模型的表单时,只发布了部分嵌套模型信息。我只得到第一个字段,而不是用户可能选择的“n”个其他字段在发布的单个字段中,没有任何行插入到我为HABTM关系创建的连接

ruby-on-rails - rails : How to test if an attribute of a class object is required in the model policy?

简单地说,我有一个模型用户,其中包含名称、电子邮件和评论作为属性。validates_presence_of:namevalidates_presence_of:email所以“姓名”和“电子邮件”是必需的,但不是“评论”。my_user=User.new我想找到一种方法来测试像my_user.name.required?或User.name.required?类似的东西。我的目标是创建一个表单,并根据该项目是否设置为“validates_presence_of”,将特定类动态添加到表单项目span或td我试图搜索它,但没有找到任何相关信息。有没有简单的方法可以做到这一点?谢谢

ruby-on-rails - accepts_nested_attributes_for 忽略空白值

我有classProfilehas_many:favorite_books,:dependent=>:destroyhas_many:favorite_quotes,:dependent=>:destroyaccepts_nested_attributes_for:favorite_books,:allow_destroy=>trueaccepts_nested_attributes_for:favorite_quotes,:allow_destroy=>trueend我有一个动态表单,您可以在其中按“+”添加新的文本区域以创建新的收藏夹。我想要做的是忽略空白的,我发现这比非嵌套属性更

jquery - Rails accepts_nested_attributes_for 与 f.fields_for 和 AJAX

我很好奇如何正确使用accepts_nested_attributes_for和f.fields_for。views/orders/new.html.erbDetailsviews/order_details/_details.html.erb$$$→|length:|width:|height:|weight:controllers/orders_controller.rb(我很确定这是错误的......非常感谢这里的任何帮助)defcreate@order=Order.create(params[:order])if@order.saveflash[:success]=

ruby - 使用 YARD 记录模型属性

我正在使用YARD使用makrdown作为脚本解析器为我的Rails应用程序生成文档。大多数文档功能开箱即用。但是,我还想将模型属性记录为一个,记录模型上的可用属性列表和两个,以描述它们的语义。我无法在YARD中找到对此的任何特殊支持,我基本上只能在类注释中简单地列出属性。有没有办法记录动态生成的模型属性,以便它们像标准属性/方法一样出现在文档中?附言我已经使用annodate-modelsgem在类列表的顶部生成基本模式转储,但这并不是我真正想要的。 最佳答案 似乎YARD现在有自己的@!attribute(注意感叹号)标签用于此

ruby-on-rails - 在 Rails 3 中使用 update_columns?

在Rails3中有更短的方法吗?user.update_column(:attribute1,value1)user.update_column(:attribute2,value2)user.update_column(:attribute3,value3)user.update_column(:attribute4,value4)我试过update_columns但它只在Rails4中可用。感谢您的帮助。 最佳答案 这是Rails3.x的解决方法:User.where(id:user.id).update_all(attribu

ruby-on-rails - 警告 : Can't mass-assign protected attributes

当发帖到/:username/about时,我收到“警告:无法批量分配protected属性:about”。classAbout["lower(username)=?",params[:username].downcase])iftrue@about=@user.aboutif@about.update_attributes(params[:about])flash[:notice]="Successfullyupdatedpost."respond_with(@about,:location=>about_path(@about.user.username))elseredirect