nested-attributes

first_or_create by email and then save the nested model

[亡魂溺海] 提交于 2019-12-03 13:08:47
I two models User and Submission as follows: class User < ActiveRecord::Base # Associations has_many :submissions accepts_nested_attributes_for :submissions # Setup accessible (or protected) attributes for your model attr_accessible :email, :name, :role, :submission_ids, :quotation_ids, :submissions_attributes validates :email, :presence => {:message => "Please enter a valid email address" } validates :email, :uniqueness => { :case_sensitive => false } end class Submission < ActiveRecord::Base belongs_to :user attr_accessible :due_date, :text, :title, :word_count, :work_type, :rush, :user,

How to decorate nested attributes (associations) with Draper in Rails 3?

冷暖自知 提交于 2019-12-03 11:37:06
My environment: Rails 3.2 with draper gem I'm using nested resources and having trouble figuring out where to declare the decorator. #app/controllers/users_controller.rb def show @user = UserDecorator.find(params[:id]) @items = @user.items end #app/controllers/items_controller.rb def show @item = @user.items.find(params[:id]) end I tried replacing items with ItemDecorator and it didn't work. Where should I be putting it? I know that Draper has issues with nested resources in forms, but this isn't a form! As far as I've understood your problem correctly, you've a model user which has many items

Rails 4 deleting nested attributes, works on create but not on edit/update

橙三吉。 提交于 2019-12-03 11:31:22
问题 So I'm working from this Railscast. And I'm aware that there have been some changes in Rails 4 for Strong parameters. First relevant question. Second relevant question I've quadruple checked my implementation, but can't see where I'm going wrong. As it is at the moment, ticking the "destroy" box when submitting the patient initially (i.e. the create method) works as intended, and will delete any medication that has a checked box and permitting any that does not (from the three form inputs it

simple_fields_for not showing up [rails 4]

孤街醉人 提交于 2019-12-03 11:04:27
Im trying to create two hidden fields, and one shows up no problem, but the other that comes from the nested form does not product.rb class Product < ActiveRecord::Base has_many :product_options, dependent: :destroy accepts_nested_attributes_for :product_options, allow_destroy: true, :reject_if => proc { |x| x[:option_name].blank? } belongs_to :user end product_option.rb class ProductOption < ActiveRecord::Base belongs_to :product end products_controller.rb class ProductsController < ActionController::Base layout "application" def index @products = Product.all @current_user = Client.find_by(id

Rails - Validate Nested Attributes Uniqueness with scope parent of parent

好久不见. 提交于 2019-12-03 10:17:22
问题 I have a problem with the scoped uniqueness validation in Rails for nested attributes with a parent of parent. Background I have a rails 4 application with 3 models : #app/models/account.rb class Account < ActiveRecord::Base has_many :contacts, dependent: :destroy end #app/models/contact.rb class Contact < ActiveRecord::Base belongs_to :account has_many :email_addresses, dependent: :destroy, validate: :true, inverse_of: :contact accepts_nested_attributes_for :email_addresses,allow_destroy:

Rails - How to manage nested attributes without using accepts_nested_attributes_for?

浪尽此生 提交于 2019-12-03 08:33:30
My problem is I've run into limitations of accepts_nested_attributes_for, so I need to figure out how to replicate that functionality on my own in order to have more flexibility. (See below for exactly what's hanging me up.) So my question is: What should my form, controller and models look like if I want to mimmic and augment accepts_nested_attributes_for? The real trick is I need to be able to update both existing AND new models with existing associations/attributes. I'm building an app that uses nested forms. I initially used this RailsCast as a blueprint (leveraging accepts_nested

Rails 4 NOT updating nested attributes

冷暖自知 提交于 2019-12-03 05:52:02
Issue: Instead of updating nested attributes, they are being created on top of the existing nested attributes when I hit the #update action of the associated features_controller.rb Likely Cause: I think the problem lies in my lack of understanding in Rails' form_for . I think the breakdown is in my views, how I render the persisting nested attributes, and/or how I fail to specify the nested attribute's id, causing it to simply create a new one feature.rb class Feature < ActiveRecord::Base ... has_many :scenarios accepts_nested_attributes_for :scenarios, allow_destroy: true, reject_if: :all

Alternative for accepts_nested_attributes_for - maybe virtus

心不动则不痛 提交于 2019-12-03 04:55:51
I'm relatively new to rails and finally found the right way to use accepts_nested_attributes_for . However, there are some serious resources on the web who say that using accepts_nested_attributes_for is generally a bad practice (like this one ). What changes are necessary to avoid accepts_nested_attributes_for and in which folder would you put the additional class-file (I guess one needs an additional class). I read that virtus is appropriate for that. Is that right? Here is a very basic example still using accepts_nested_attributes_for (find the full example here ): Models class Person <

Better solution for nested Backbone.js collections

吃可爱长大的小学妹 提交于 2019-12-03 03:13:45
问题 Many of my Backbone models often deal with nested models and collections, so far I'm using a combination of defaults , parse and toJSON manually to achieve nesting: ACME.Supplier = Backbone.Model.extend({ defaults: function() { return { contacts: new ACME.Contacts(), tags: new ACME.Tags(), attachments: new ACME.Attachments() }; }, parse: function(res) { if (res.contacts) res.contacts = new ACME.Contacts(res.contacts); if (res.tags) res.tags = new ACME.Tags(res.tags); if (res.attachments) res

WARNING: Can't mass-assign protected attributes

血红的双手。 提交于 2019-12-03 02:53:11
I get this error "WARNING: Can't mass-assign protected attributes: races_attributes" , when following this http://railscasts.com/episodes/196-nested-model-form-part-1 on rails 3. Where Races are a component of Events. This is my models/race.rb: class Race < ActiveRecord::Base belongs_to :event attr_accessible :name, :unit end This is my models/event.rb: class Event < ActiveRecord::Base has_many :races, :dependent => :destroy accepts_nested_attributes_for :races attr_accessible :name, :date, :description, :location_name, :address_one, :address_two, :city, :state, :zip, :active, :races