标签: nested-attributes

嵌套属性和通过控制器添加属性?

由于这个问题很难描述,这是我能提出的最佳标题,所以这里有一些代码.

鉴于三个模型Parent,Child && Grandchild.

Parent <  ActiveRecord::Base
  has_many :children
  has_many :grandchildren
  accepts_nested_attributes_for :child
end

Child <  ActiveRecord::Base
  belongs_to :parent
  has_many :kids, :as => :grandchildren #this is just an example
  accepts_nested_attributes_for :grandchild
end

Grandchild <  ActiveRecord::Base
  belongs_to :parent
  belongs_to :child
end
Run Code Online (Sandbox Code Playgroud)

我想将current_user.id添加到在Parent#new期间创建的子记录和Grandchild记录中.我现在使用隐藏字段,因为我找不到添加它们的好方法.

也许有人可以通过创建回调来在创建时添加current_user.id来提供帮助?无论如何,我从来没有太多运气进入模型,但你很聪明.

思考?

ruby-on-rails nested-attributes

3
推荐指数
1
解决办法
1499
查看次数

如何在一个关系中设计使用accepts_nested_attributes_for?

我试图让我的用户表单也允许用户通过form_for同时填写他们的公司资料.由于某种原因,它没有显示公司领域.这是我的控制器和布局代码.

class User < ActiveRecord::Base
  attr_accessible :company_attributes

  has_one :company
  accepts_nested_attributes_for :company
end

class Company < ActiveRecord::Base
  belongs_to :user

  # Validation
  validates :name, :presence => true
end

<%= f.fields_for :company do |company_form| %>
  <div class="field">
    <%= company_form.label :name, "Company Name" %><br />
    <%= company_form.text_field :name %>
  </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails has-one nested-attributes devise ruby-on-rails-3

3
推荐指数
2
解决办法
2355
查看次数

带有mongoid的rails 3中的嵌套属性问题(嵌套对象未保存)

所以我在RailsCast中有一个关于嵌套表单的简单应用程序.问题是,当我提交表格(带有调查和问题)时,问题不会被保存.

我的模型(调查,有很多问题):

class Survey
  include Mongoid::Document
  field :name
  has_many :questions, :dependent => :destroy
  accepts_nested_attributes_for :questions, :allow_destroy => true
end

class Question
  include Mongoid::Document
  field :content
  belongs_to :survey
end
Run Code Online (Sandbox Code Playgroud)

和调查控制员:

def new
    @survey = Survey.new
    3.times {@survey.questions.build}
....
Run Code Online (Sandbox Code Playgroud)

和一个观点:

<%= form_for(@survey) do |f| %>
    <%= f.fields_for :questions do |builder| %>
    <%= builder.label :content, "Question" %><br />
    <%= builder.text_area :content, :rows => 3 %><br />
    <%= builder.check_box :_destroy %>
    <%= builder.label :_destroy, "Remove Question" …
Run Code Online (Sandbox Code Playgroud)

nested-attributes mongoid ruby-on-rails-3

3
推荐指数
1
解决办法
2964
查看次数

Rails 4.0和Cocoon:fields_for下的嵌套字段不会出现在Edit中

希望有人可以在这里建议修复.我对Rails相当新,并探索Rails 4.0中的变化.我在Rails 4.0中构建了这个简单的食谱书应用程序.我有食谱的主要模型(名称,cook_time,oven_temp,说明等).因为一些食谱可能有5种成分而其他食谱可能有20种,我想在一个带有has_many关联的单独模型中打破成分.所以食谱has_many成分和accepts_nested_attributes_for:成分.以下是模型:

recipe.rb

class Recipe < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  has_many :ingredients, :dependent => :destroy

  validates :name, presence: true, length: { maximum: 50 }

  accepts_nested_attributes_for :ingredients, 
                :reject_if => lambda { |a| a[:name].blank? },
                            :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)

ingredient.rb

class Ingredient < ActiveRecord::Base
  belongs_to :recipe
end
Run Code Online (Sandbox Code Playgroud)

提醒:Rails 4.0不再使用attr_accessible,而是使用强params将赋值移动到控制器中.

recipes_controller.rb

class RecipesController < ApplicationController
  before_action :find_recipe, only: [:show, :edit, :update, :destroy]
  before_action :set_current_user, except: [:index, :show]
  respond_to :html, :js

  ...

  def edit
  end

  def update
    if @recipe.update_attributes(recipe_params)
      redirect_to @recipe
    else …
Run Code Online (Sandbox Code Playgroud)

gem nested-attributes fields-for ruby-on-rails-4 cocoon-gem

3
推荐指数
1
解决办法
3375
查看次数

为什么不更新rails中嵌套表单字段的操作?

我有一个属于个人资料模型的工作模型.配置文件has_many工作.我有一个嵌套的模型表单,用户可以在其中添加作业.表单已成功添加作业,但编辑/更新无效.相反,当我尝试编辑作业时,它会保留旧版本的作业,但也会添加新版本.它不会用新版本替换旧版本.我该如何解决?我很确定它与编辑/更新控制器有关.

编辑控制器:

def edit
    @profile = current_user.profile
end
Run Code Online (Sandbox Code Playgroud)

更新控制器:

def update
    #if current_user.profile.jobs.any?

    @profile = current_user.profile.update_attributes(profile_params)

    if current_user.profile.invalid?
        @profile = current_user.profile
        render :edit, :status => :unprocessable_entity
    else
        redirect_to profile_path(current_user.profile_name)
    end
end
Run Code Online (Sandbox Code Playgroud)

问题是,更新控制器正在处理非嵌套信息,它不适用于嵌套作业.以下是强大的参数:

def profile_params
      params.require(:profile).permit(:title,
           :category, :description, :state, :zip_code, :rate, 
           jobs_attributes: [:firm, :position, :category, :description,
           :begin, :end, :_destroy])        
end
Run Code Online (Sandbox Code Playgroud)

这是配置文件模型:

class Profile < ActiveRecord::Base
    belongs_to :user
    has_many :jobs
    accepts_nested_attributes_for :jobs , :reject_if => :all_blank, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)

此外,这是我的路线,如果这将有所帮助:

resources :profiles do
   resources :jobs
end
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助.

编辑

以下是创建动作中的参数:

{"jobs_attributes"=>{"1383593195849"=>{"firm"=>"1st firm", …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-forms nested-attributes ruby-on-rails-4 rails-activerecord

3
推荐指数
1
解决办法
1028
查看次数

我无法使用accepts_nested_attributes_for创建模型对象.它不会创建嵌套对象

我的模型结构如下所示:

董事会has_many主题.主题has_many帖子.

应用程序/模型/ board.rb

class Board < ActiveRecord::Base
    has_many :topics
end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ topic.rb

class Topic < ActiveRecord::Base
    belongs_to :user
    belongs_to :board
    has_many :posts

    accepts_nested_attributes_for :posts

    validates :title, presence: true, length: { maximum: 255 }
    validates :user_id, presence: true
    validates :board_id, presence: true
    ...
end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ post.rb

class Post < ActiveRecord::Base
    belongs_to :user
    belongs_to :topic

    validates :user_id, presence: true
    validates :topic_id, presence: true
    validates :content, length: { minimum: 8 }
end
Run Code Online (Sandbox Code Playgroud)

以下是我创建新主题的观点.fields_for部分用于在新帖子上创建:内容

应用程序/视图/主题/ new.html.erb

<div>
    <%= form_for [@board, @topic] do |f| …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails associations nested-resources nested-attributes ruby-on-rails-4

3
推荐指数
1
解决办法
1847
查看次数

通过复选框Rails 4添加多个嵌套属性(可能有多个表单)

3/13更新:
我用我的模型,控制器逻辑和几个表单版本做了一个小样本项目.



我正在构建一个表单,用户可以在其中添加"任务"和"里程碑".(即.任务='真空'在里程碑里面''干净的房子').它基本上是一个任务/子任务类型模型,父级为"里程碑",子级为"任务".

任务和里程碑都属于"项目"....所以我试图通过嵌套表单添加任务和里程碑以及更新操作.我想要的方法是为每个@task_template实例创建一个表单并一次更新多个表单.

我的问题是我也试图通过名为"MilestoneTemplates"和"TaskTemplates"的表动态设置"启动里程碑/任务"....

用户提取"添加里程碑/任务"页面,并根据其类型项目,他们在复选框旁边看到一系列预建任务(@task_templates)和里程碑(@milestone_templates).然后,用户检查他们要添加的任务或里程碑旁边的复选框.这应该为用户创建一个具有预建@ task_template.name,@ task_template.description ...等的特定任务

我甚至无法创建1.我正在使用Rails 4,我认为我已经正确设置了我的strong_params.以下是我对此的看法:

楷模:

class Task < ActiveRecord::Base
    belongs_to :user
    belongs_to :project
  belongs_to :milestone

class Milestone < ActiveRecord::Base
 belongs_to :project
 belongs_to :user
 has_many :tasks, dependent: :destroy, inverse_of: :milestone
 accepts_nested_attributes_for :tasks, allow_destroy: true

class Project < ActiveRecord::Base
 has_many :milestones, dependent: :destroy
 has_many :tasks, dependent: :destroy
 accepts_nested_attributes_for :tasks, allow_destroy: true
 accepts_nested_attributes_for :milestones, allow_destroy: true

 #the "Starter Milestones & Tasks"

class MilestoneTemplate < ActiveRecord::Base
    has_many :task_templates, dependent: :destroy, inverse_of: :milestone_template

class TaskTemplate < ActiveRecord::Base
     belongs_to …
Run Code Online (Sandbox Code Playgroud)

forms checkbox ruby-on-rails nested-attributes strong-parameters

3
推荐指数
1
解决办法
6729
查看次数

Rails4:如何在使用accepts_nested_attributes_for时触发attr_accessor的回调

请检查伪代码:

class Team
  has_many :users
  accepts_nested_attributes_for :users, allow_destroy: true
end

class User
  belongs_to :team
  has_many :addresses
  accepts_nested_attributes_for :addresses
  attr_accessor :dummy

  before_validation :generate_addresses_attributes
  def generate_addresses_attributes
    # Use the dummy value to set the addresses_attributes
  end
end
Run Code Online (Sandbox Code Playgroud)

现在执行时team.update(users_attributes: [{"0" => { dummy: "changed!" }}])(其他字段除了dummy属性不会改变),它不会触发#generate_addresses_attributes回调,因为它认为没有任何改变,没有保存,没有回调......

所以我的问题是如何触发虚拟属性的回调,或者强制save为accept_nested_attributes_for.

谢谢!

callback nested-attributes attr-accessor ruby-on-rails-4

3
推荐指数
2
解决办法
1214
查看次数

rails4集合通过关联和嵌套模型表单选择has_many

我有一个rails4应用程序.目前,我的收藏选择仅在我选择一个选项时才有效.以下是我的工作代码.我只有产品表格.行业模型填充seeds.rb.IndustryProduct仅用于连接其他2个型号.

我想知道我必须在代码中更改哪些内容才能选择更多内容.

我看到一些工作示例的multiple: true选项如(https://www.youtube.com/watch?v=ZNrNGTe2Zqk 10:20)但在这种情况下,用户界面有点难看+无法用任何样本将其拉下来码.是否还有其他解决方案,例如选择一个选项而不是一个包含多个选项的方框?

楷模:

class Product < ActiveRecord::Base
  belongs_to :user
  has_many :industry_products
  has_many :industries, through: :industry_products
  has_many :product_features

  accepts_nested_attributes_for :industry_products, allow_destroy: true
  accepts_nested_attributes_for :product_features

  validates_associated :industry_products
  validates_associated :product_features
end

class Industry < ActiveRecord::Base
  has_many :industry_products
  has_many :products, through: :industry_products

  accepts_nested_attributes_for :industry_products
end

class IndustryProduct < ActiveRecord::Base
  belongs_to :product
  belongs_to :industry
end
Run Code Online (Sandbox Code Playgroud)

_form.html.erb

<%= form_for @product do |f| %>
  <%= render 'layouts/error_messages', object: f.object %>
  ......
  <%= f.fields_for :industry_products do |p| %>
    <%= p.collection_select …
Run Code Online (Sandbox Code Playgroud)

forms activerecord ruby-on-rails nested-attributes collection-select

3
推荐指数
1
解决办法
4383
查看次数

AtributteError"对象没有属性" - 如何防止没有try/except - 访问模型字段

我有一个问题:(我正在研究Django 1.8,Python 2.7.15)我从数据库中获取了一个对象:

shop_users = ShopUsers.objects.get(pk=_id)
Run Code Online (Sandbox Code Playgroud)

然后,如果对象存在,我正在为View准备数据:

if shop_users:
    data = {
        'full_name': shop_users.full_name,
        'shop': shop_users.shop.title,
        'price_title': shop_users.price.title if shop_users.price.title else '',
        'package_price': shop_users.price.price,
        'user_price': shop_users.payment.operation_amount
    }
Run Code Online (Sandbox Code Playgroud)

但是shop_users.price.title可能不存在.

我想在我准备上面的数据时检查它(我正在做'...如果......其他'),但如果shop_users.price.title不存在则提供AttributeError.

我可以在'data'声明之前使用try/except但这会使我的代码加倍...

有没有处理AttributeError的技巧(... if ... else)?

也许shop_users.price.title [0](不起作用)

或者获取(shop_users.price.title)?

我只是不想加倍我的代码...但我不知道任何诀窍:/

我很年轻.我感谢任何帮助!

python django attributes nested-attributes

3
推荐指数
1
解决办法
1038
查看次数