nested-attributes

Rails 4: accepts_nested_attributes_for and mass assignment

空扰寡人 提交于 2019-11-28 11:07:42
I am trying to reproduce railscast #196 in Rails 4. However, I'm experiencing some problems. In my example I try to generate a Phonebook - each Person could have multiple PhoneNumbers These are important parts of my controller: class PeopleController < ApplicationController def new @person = Person.new 3.times{ @person.phones.build } end def create @person = Person.create(person_params) @person.phones.build(params[:person][:phones]) redirect_to people_path end private def person_params params.require(:person).permit(:id, :name, phones_attributes: [ :id, :number ]) end end and this is my new

accepts_nested_attributes_for to link to existing record, not create a new one

醉酒当歌 提交于 2019-11-28 07:35:56
I have the following models class Order < AR::Base has_many :products accepts_nested_attributes_for :products end class Product < AR::Base belongs_to :order has_and_belongs_to_many :stores accepts_nested_attributes_for :stores end class Store < AR::Base has_and_belongs_to_many :products end Now I have a order view where I want to update the shops for the product. The thing is that I only want to connect the products to the existing shops in my db, not create new ones. My form in the order view looks like this (using Formtastic): = semantic_form_for @order do |f| = f.inputs :for => :live

accepts_nested_attributes_for with has_many => :through Options

六眼飞鱼酱① 提交于 2019-11-28 05:50:37
I have two models, links and tags, associated through a third, link_tags. The following code is in my Link model. Associations: class Link < ActiveRecord::Base has_many :tags, :through => :link_tags has_many :link_tags accepts_nested_attributes_for :tags, :allow_destroy => :false, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } end class Tag < ActiveRecord::Base has_many :links, :through => :link_tags has_many :link_tags end class LinkTag < ActiveRecord::Base belongs_to :link belongs_to :tag end links_controller Actions: def new @link = @current_user.links.build respond_to do

Rails 3 + JQuery-File-Upload + Nested Model

北慕城南 提交于 2019-11-28 03:52:16
I've been searching for some examples, but have come up short: I'm trying to implement JQuery-File-Upload on a project I'm working on, but am getting lost as to how to get it to work with nested attributes. Quick overview: 2 Models: Comment [has_many :attachments] Attachment [belongs_to :comment] Comment accepts_nested_attributes_for :attachments . Also - I'm using Dragonfly. I've reviewed the Rails 3 guides on the JQuery-File-Upload site, but they assume it's a singular model, so it's all built around a form. Does anyone have any examples of their implementation or is there an existing

accepts_nested_attributes_for ignore blank values

两盒软妹~` 提交于 2019-11-27 23:41:29
问题 i have class Profile has_many :favorite_books, :dependent => :destroy has_many :favorite_quotes, :dependent => :destroy accepts_nested_attributes_for :favorite_books, :allow_destroy => true accepts_nested_attributes_for :favorite_quotes, :allow_destroy => true end I have a dynamic form where you press '+' to add new textareas for creating new favorites. What i want to do is ignore the blank ones, I find this harder to sort through in the update controller than a non nested attribute. What i

How should I use rails and simple_form for nested resources?

纵然是瞬间 提交于 2019-11-27 23:34:04
I'm trying to create one resource with another nested resource at the same time. I'm using Rails4 and simple_form 3.0.0rc. Here is my code. Models: class User < ActiveRecord::Base has_one :profile accepts_nested_attributes_for :profile end class Profile < ActiveRecord::Base belongs_to :user end Controller: class UsersController < ApplicationController def new @user = User.new @user.build_profile end def create user = User.new user_params user.save redirect_to root_url # @par =params end private def user_params params.require(:user).permit(:email, profile_attributes: [:name]) end end View (form

RoR nested attributes produces duplicates when edit

孤街浪徒 提交于 2019-11-27 18:05:01
I'm trying to follow Ryan Bates RailsCast #196: Nested model form part 1 . There're two apparent differences to Ryans version: 1) I'm using built-in scaffolding and not nifty as he's using, and 2) I'm running rails 4 (I don't really know what version Ryans using in his cast, but it's not 4). So here's what I did rails new survey2 cd survey2 bundle install rails generate scaffold survey name:string rake db:migrate rails generate model question survey_id:integer content:text rake db:migrate Then I added the associations to the models like so class Question < ActiveRecord::Base belongs_to :survey

Use rails nested model to *create* outer object and simultaneously *edit* existing nested object?

放肆的年华 提交于 2019-11-27 17:53:43
Using Rails 2.3.8 Goal is to create a Blogger while simultaneously updating the nested User model (in case info has changed, etc.), OR create a brand new user if it doesn't exist yet. Model: class Blogger < ActiveRecord::Base belongs_to :user accepts_nested_attributes_for :user end Blogger controller: def new @blogger = Blogger.new if user = self.get_user_from_session @blogger.user = user else @blogger.build_user end # get_user_from_session returns existing user # saved in session (if there is one) end def create @blogger = Blogger.new(params[:blogger]) # ... end Form: <% form_for(@blogger) do

Nested models and parent validation

馋奶兔 提交于 2019-11-27 17:23:49
I've got two models. - Parent has_many Children ; - Parent accepts_nested_attributes_for Children ; class Parent < ActiveRecord::Base has_many :children, :dependent => :destroy accepts_nested_attributes_for :children, :allow_destroy => true validates :children, :presence => true end class Child < ActiveRecord::Base belongs_to :parent end I use validation to validate presence of children for every parent, so I can't save parent without children. parent = Parent.new :name => "Jose" parent.save #=> false parent.children_attributes = [{:name => "Pedro"}, {:name => "Emmy"}] parent.save #=> true

Add Multiple Nested Attributes through checkboxes Rails 4 (maybe with multiple forms)

对着背影说爱祢 提交于 2019-11-27 17:00:44
问题 3/13 UPDATE: I've made a small sample project with my models, controller logic and several form versions. I am building a form where a user can add "Tasks" and "Milestones" together. (ie. Task = 'Vacuum' is inside Milestone = 'clean House'). It's basically a Task/Subtask type model with the parent being 'Milestone' and the child being 'Task'. Both Tasks and Milestones belong to "Project"....so I am trying to add the Tasks and Milestones through a nested form with an update action. I am