nested-attributes

Rails 3: How does “accepts_nested_attributes_for” work?

怎甘沉沦 提交于 2019-11-27 10:17:56
问题 Consider the following association: class Product < ActiveRecord::Base belongs_to :shop accepts_nested_attributes_for :shop end If params[:product][:shop_attributes] = {"name" => "My Shop"} and I do: @product = Product.new(params[:product]) @product.save a new shop with name "My Shop" is created and assigned to the @product , as expected. However, I can't figure out what happens when shop_attributes contains some id , like: params[:product][:shop_attributes] = {"id" => "20", "name" => "My

Does accepts_nested_attributes_for work with belongs_to?

此生再无相见时 提交于 2019-11-27 07:32:01
I have been getting all kinds of conflicting information regarding this basic question, and the answer is pretty crucial to my current problems. So, very simply, in Rails 3, is it allowed or not allowed to use accepts_nested_attributes_for with a belongs_to relationship? class User < ActiveRecord::Base belongs_to :organization accepts_nested_attributes_for :organization end class Organization < ActiveRecord::Base has_many :users end In a view: = form_for @user do |f| f.label :name, "Name" f.input :name = f.fields_for :organization do |o| o.label :city, "City" o.input :city f.submit "Submit"

Nested paperclip form with multiple images

对着背影说爱祢 提交于 2019-11-27 07:04:44
问题 I have a one-to-many association between a Banana model and an Image model. In addition, each Banana and Image belong to a User (via a separate association because an Image and its Banana might have different Users). I would like a nested form to create Bananas as well as Images. The kicker is that I don't know how many Images to build (note the multiple attribute). The commented out bit of the form below will create the appropriate amount of Images, but won't complete the associated User

Rails 4: accepts_nested_attributes_for and mass assignment

流过昼夜 提交于 2019-11-27 06:05:04
问题 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

Getting fields_for and accepts_nested_attributes_for to work with a belongs_to relationship

人走茶凉 提交于 2019-11-27 03:27:52
I cannot seem to get a nested form to generate in a rails view for a belongs_to relationship using the new accepts_nested_attributes_for facility of Rails 2.3. I did check out many of the resources available and it looks like my code should be working, but fields_for explodes on me, and I suspect that it has something to do with how I have the nested models configured. The error I hit is a common one that can have many causes: '@account[owner]' is not allowed as an instance variable name Here are the two models involved: class Account < ActiveRecord::Base # Relationships belongs_to :owner,

accepts_nested_attributes_for with has_many => :through Options

我的梦境 提交于 2019-11-27 01:05:47
问题 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

Rails 3 + JQuery-File-Upload + Nested Model

↘锁芯ラ 提交于 2019-11-27 00:12:28
问题 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

How should I use rails and simple_form for nested resources?

你离开我真会死。 提交于 2019-11-26 21:48:37
问题 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

accepts_nested_attributes_for with belongs_to polymorphic

别说谁变了你拦得住时间么 提交于 2019-11-26 19:29:30
I would like set up a polymorphic relation with accepts_nested_attributes_for . Here is the code: class Contact <ActiveRecord::Base has_many :jobs, :as=>:client end class Job <ActiveRecord::Base belongs_to :client, :polymorphic=>:true accepts_nested_attributes_for :client end When I try to access Job.create(..., :client_attributes=>{...} gives me NameError: uninitialized constant Job::Client Dmitry Polushkin I've also had a problem with the "ArgumentError: Cannot build association model_name. Are you trying to build a polymorphic one-to-one association?" And I found a better solution for this

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

旧巷老猫 提交于 2019-11-26 19:15:30
问题 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