Tag: 质量分配

如何在Rails 4中为设计注册添加自定义字段?

我正在尝试将一个字段添加到使用设计的注册页面。 我按照Devise文档中的说明进行操作。 新字段是“角色”字段。 我已成功将其迁移到数据库。 我现在正努力使注册有效。 这是新的applicationcontroller: class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception def devise_parameter_sanitizer if resource_class == User User::ParameterSanitizer.new(User, :user, params) else super # Use the default one end end end 这是更改的用户模型: class User::ParameterSanitizer < Devise::ParameterSanitizer […]

Rails:在属于其他两个模型的模型中创建一个新条目

考虑一个has_many产品的商店,其中有很多意见。 这是模特: 商店: class Store :products end 产品: class Product < ActiveRecord::Base attr_accessible :desc, :name, :status, :url belongs_to :store has_many :opinions end 最后,意见: class Opinion < ActiveRecord::Base attr_accessible :content, :eval, :status belongs_to :store belongs_to :product end 要创建新的意见(属于产品和商店),这里是OpinionsController的创建方法: def create # Get product info. product = Product.find_by_id params[:opinion][:product_id] # Create a new opinion to this product […]

Rails 3配置attr_accessible / protected的设置

我花了很长时间试图解决模型中的虚拟属性问题。 原来我只是忘了把它添加到我的模型中的attr_accesible。 当然,我应该早点或更好地抓住它,首先应该通过将它添加到attr_accessible来开始整个过程​​。 为了防止这种情况再次发生,是否有一个配置设置我可以标记为在开发时抛出exception,如果我尝试批量分配并在受保护/不可访问时validation它? 我知道我可以使用set config.active_record.whitelist_attributes = true来要求所有人使用白名单,但我的问题更多的是基于个别属性。 例如,如果我有一个带有attr_accessible的模型,则上面的行不会发出警告:name然后添加:nickname(virtual或not),并尝试批量分配它,检查presence => true。 我希望它警告我,我试图通过质量分配来validation受保护的属性。

从JSON反序列化ActiveRecord

我想使用JSON序列化将查询结果保存到redis中并查询它。 将查询结果提供给json非常简单: JSON.generate(Model.all.collect {|item| item.attributes}) 但是我没有找到将其反序列化回ActiveRecord的正确方法。 最直接的方式: JSON.parse(@json_string).collect {|item| Model.new.from_json(item)} 给我一个错误: WARNING: Can’t mass-assign protected attributes: id 所以id变空了。 我想过只使用OpenStruct代替ActiveRecord,但我相信有更好的方法。

Delayed_job(2.1.4)错误:作业无法加载:需要IO实例。 处理者无

我创建了一个简单的成就系统,并想引入delayed_job(2.1.4)来处理。 但是,delayed_jobs表中的处理程序列始终为nil,这会导致last_error文本: Job failed to load: instance of IO needed. Handler nil Job failed to load: instance of IO needed. Handler nil 这是我的设置: 成就观察员 class AchievementObserver < ActiveRecord::Observer observe User, Comment, … def after_create(record) # initiate delayed job to check conditions Delayed::Job.enqueue(TrophyJob.new(record.id, record.class.name)) end … end 奖杯工作 class TrophyJob < Struct.new(:record_id, :record_type) def perform case […]

Rails – Accepts_nested_attributes_for质量分配错误

我目前正在尝试在belongs_to关系上设置一个带有嵌套字段的表单,但是我遇到了一个质量分配错误。 到目前为止,我的代码如下(删除了一些html): 促销型号: class Sale < ActiveRecord::Base attr_accessible :customer_attributes belongs_to :customer accepts_nested_attributes_for :customer end new.html.erb: sales_path do |sale| -%> “customers/form”, :locals => {:customer => customer_builder, :form_actions_visible => false} %> 客户/ _form.html.erb Customer Type “chzn-select”}) %> 我相信这应该允许我创建一个Sale对象和一个嵌套的Customer对象。 发送的参数是(注意包括一些不相关的参数): {“utf8″=>”✓”, “authenticity_token”=>”qCjHoU9lO8VS060dXFHak+OMoE/GkTMZckO0c5SZLUU=”, “customer”=>{“customer_type_id”=>”1”}, “sale”=>{“customer”=>{“features_attributes”=>{“feature_type_id”=>”1”, “value”=>”jimmy”}}}, “vehicle”=>{“trim_id”=>”1”, “model_year_id”=>”1”}} 我得到的错误是: Can’t mass-assign protected attributes: customer 我可以看到为什么会出现这种情况,因为:客户不在销售的attr_accessible列表中 – 尽管表单不应该发送customer_attributes而不是customer? 任何帮助/建议表示赞赏。 编辑1:据我所知,Sale模型中的attr_accessible应该包含:customer_attributes […]

嵌套表单触发’无法批量分配受保护属性警告

我有一个多层嵌套表单 用户 – >任务 – >前提条件 并以相同的forms 用户 – >任务 – >位置 位置表单工作正常,现在我正在尝试指定当前任务的先决条件。 先决条件是存储在:completed_task字段中的task_id。 当我提交表单时,我在输出中收到以下错误 警告:无法批量分配受保护的属性:prerequisite_attributes 对用户中的每个任务发出一个警告。 我已经完成了与此相关的所有其他问题,确保正确引用字段名称:completed_task, 将attr_accessible添加到我的模型中(它已经存在并且我扩展了它)。 我不确定我应该做什么。 我的模特看起来像 class Task <ActiveRecord :: Base attr_accessible:user_id,:date,:description,:location_id belongs_to:用户 has_one:location accepts_nested_attributes_for:location has_many:先决条件 accepts_nested_attributes_for:先决条件 结束 class先决条件<ActiveRecord :: Base attr_accessible:completed_task belongs_to:任务 结束 表格使用formtastic,我包括表格via builder3%> — _prerequisite_fields.html.erb —– 有什么建议?

当我们只更新一个属性时,我们应该使用强params吗?

我正在开发一个Rails应用程序,我有几个操作(#delete_later,#ban_later等等),我只从请求参数中设置一个属性(具体来说,是执行该操作的reason字段)。 我想知道是否可以这样做: def ban_later @object.reason = params[:object][:reason] @object.save end 或者即使在这种情况下使用强对数也是最佳做法? def ban_later @object.reason = object_params[:reason] @object.save end private def object_params params.require(:object).permit(:permitted_1, :permitted_2, :reason) end 哪种解决方案最好? 如果没有,那么问题的最佳解决方案是什么? 后来编辑: #ban_later,#delete_later操作确实可以设置标志列status但是可以在不从params散列中接收它的值的情况下完成。 由于您只为每个方法设置一个状态,因此当您在#delete_later时可以简单地设置状态“pending_delete”,当您在#ban_later时,可以设置“pending_ban”。 后来编辑 为什么直接使用#save而不是update_attributes ? 假设你需要一个if @object.save语句。 在false分支(对象未保存)上,您可能仍希望呈现使用该@object内容的视图。

Ruby on Rails:循环迭代的批量分配?

我得到了质量分配错误。 Can’t mass-assign protected attributes: 1, 2, 3, 4, 5, 6, 7 这些数字代表此循环中的迭代: 这是在我的模型中: attr_accessible :day, :open_time, :close_time 我正在尝试创建一个这样的数组: “hour”=>{ “1”=>{“day”=>”Sunday”,”open_time”=>”6″,”close_time”=>”6″}, “2”=>{“day”=>”Sunday”,”open_time”=>”6″,”close_time”=>”6″}, “3”=>{“day”=>”Sunday”,”open_time”=>”6″,”close_time”=>”6″} } 我正在尝试将新行中的每次迭代保存到数据库中 def create @hour = @hourable.hours.new(params[:hour]) end 如何修复迭代质量分配? 或者我这样做是错的? 谢谢!

simple_form中的嵌套属性返回质量分配错误

楷模: class Topic :destroy validates :name, :presence => true, :length => { :maximum => 32 } attr_accessible :name, :post_id end class Post true has_many :comments, :dependent => :destroy accepts_nested_attributes_for :topic attr_accessible :name, :title, :content, :topic, :topic_attributes end 视图: { :controller => :posts, :action => “create” } do |f| %> Create a Post false, :placeholder => […]