nested-attributes

Plural for fields_for has_many association not showing in view

别等时光非礼了梦想. 提交于 2019-12-05 10:36:53
Currently, an Item belongs_to a Company and has_many ItemVariants . I'm trying to use nested fields_for to add ItemVariant fields through the Item form, however using :item_variants does not display the form. It is only displayed when I use the singular. I have check my associations and they seem to be correct, could it possibly have something to do with item being nested under Company, or am I missing something else? Thanks in advance. Note: Irrelevant code has been omitted from the snippets below. EDIT: Don't know if this is relevant, but I'm using CanCan for Authentication. routes.rb

Can reject_if be used to reject a nested resource if all fields except one is blank?

假装没事ソ 提交于 2019-12-04 23:51:30
问题 I know that you can have: accepts_nested_attributes_for :foo, :reject_if => proc { |a| a[:bar].blank? } Is there a way to instead say something like accepts_nested_attributes_for :foo, :reject_if => blah[:bar].blank? and flah[:bar].blank? or accepts_nested_attributes_for :foo, :reject_if => all fields except record_date.blank? Thanks 回答1: I'm a bit late on this, but you can do : accepts_nested_attributes_for :foo, reject_if: ->(attributes){ attributes.except(:key).values.all?( &:blank? ) }

Nested attributes not working creating children with new parent

允我心安 提交于 2019-12-04 17:57:25
I have two models: class Shift < ActiveRecord::Base attr_accessible :ranges_attributes has_many :ranges accepts_nested_attributes_for :ranges, allow_destroy: true end class Range < ActiveRecord::Base belongs_to :shift validates :shift, presence: true end When, in my controller, I want to create a shift with ranges I'm getting: Shift.create! params[:shift] #ActiveRecord::RecordInvalid Exception: Validation failed: Shift ranges shift can't be blank If I remove validates :shift, presence: true from Range model this works beautifully. I'm able to create a new shift with his children. ActiveRecord

nested attributes in simple_form returns mass assignment error

隐身守侯 提交于 2019-12-04 15:27:04
Models: class Topic < ActiveRecord::Base has_many :posts, :dependent => :destroy validates :name, :presence => true, :length => { :maximum => 32 } attr_accessible :name, :post_id end class Post < ActiveRecord::Base belongs_to :topic, :touch => true has_many :comments, :dependent => :destroy accepts_nested_attributes_for :topic attr_accessible :name, :title, :content, :topic, :topic_attributes end View: <%= simple_form_for :post, :url => { :controller => :posts, :action => "create" } do |f| %> <h1>Create a Post</h1> <%= f.input :name, :label => false, :placeholder => "Name" %> <%= f.input

RecordNotFound with accepts_nested_attributes_for and belongs_to

不想你离开。 提交于 2019-12-04 09:53:41
I get ActiveRecord::RecordNotFound: Couldn't find Client with ID=3 for Order with ID= when trying to submit an Order form for an existing client. This happens through the form or the console by typing: Order.new(:client_attributes => { :id => 3 }) payment_form.html.erb : <%= semantic_form_for @order, :url => checkout_purchase_url(:secure => true) do |f| %> <%= f.inputs "Personal Information" do %> <%= f.semantic_fields_for :client do |ff| %> <%= ff.input :first_name %> <%= ff.input :last_name %> <!-- looks like semantic_fields_for auto-inserts a hidden field for client ID --> <% end %> <% end

Can't mass-assign protected attributes for creating a has_many nested model with Devise

社会主义新天地 提交于 2019-12-04 03:31:37
I've watched the RailsCast, another nested attributes video, lots of SO posts, and fought with this for a while, but I still can't figure it out. I hope it's something tiny. I have two models, User (created by Devise), and Locker (aka, a product wishlist), and I'm trying to create a Locker for a User when they sign up. My login form has a field for the name of their new Locker (aptly called :name ) that I'm trying to assign to the locker that gets created upon new user registration. All I'm ever greeted with is: WARNING: Can't mass-assign protected attributes: locker I've tried every

Adding a delete link for nested attributes

柔情痞子 提交于 2019-12-04 03:12:37
问题 I have nested attributes in my show.erb and I create a blank nested attribute and show a grid of items with the blank at the bottom like so. <%= form_for @question do |q| %> <% q.fields_for :answers, @question.answers do |l| %> <tr> <td><%= l.text_field :text %></td> <td><%= l.check_box :correct %></td> <td><%= l.text_field :imagename %></td> <td><%= l.number_field :x %></td> <td><%= l.number_field :y %></td> </tr> <% end %> <tr> <td colspan=5 align=right><%= submit_tag '+' %> </tr> <% end %>

ActiveRecord::AssociationTypeMismatch when attempting to save nested attributes in Rails

醉酒当歌 提交于 2019-12-04 01:33:14
问题 I read through a lot of pages on `has_one relationships and nested attributes but haven't been successful in making this work. Any help would be fantastic. Each User has_one Network. I'm trying to gather information for both attributes in one form but keep getting the exception ActiveRecord::AssociationTypeMismatch in UsersController#create The parameters passed are: {"utf8"=>"✓", "authenticity_token"=>"I54tm1ovzHEHaXbBLTT+5tqBJv2795sKg978ot3HDBc=", "user"=>{"name"=>"Bilbo Baggins", "email"

Many-to-Many Nested Attributes in Rails 4 (with strong parameters)

泄露秘密 提交于 2019-12-03 20:09:11
I have been trying to figure this one out for a few days now. I am using Rails 4 (with the updated mass assignment technique) and trying to use nested attributes with a many-to-many relationship. My record is saving to the DB but everything is nil and I'm getting an "Unpermitted parameters: school, alumnis, prospects" error in my logs. Here's what I have: referral.rb class Referral < ActiveRecord::Base belongs_to :school belongs_to :alumni belongs_to :prospect end alumni.rb class Alumni < ActiveRecord::Base has_many :referrals has_many :prospects, through: :referrals accepts_nested_attributes

Rails 4 NOT updating nested attributes

妖精的绣舞 提交于 2019-12-03 15:17:46
问题 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 ...