标签: nested-attributes

accepted_nested_attributes_for with has_many =>:通过选项

我有两个模型,链接和标签,通过第三个link_tags关联.以下代码位于我的链接模型中.

协会:

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
Run Code Online (Sandbox Code Playgroud)

links_controller动作:

  def new
    @link = @current_user.links.build
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @link }
    end
  end

  def create
    @link = @current_user.links.build(params[:link])

    respond_to do …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails has-many-through nested-attributes

26
推荐指数
3
解决办法
3万
查看次数

rails ActiveAdmin嵌套表单has_one accepts_attributes_for formtastic issue

我正在使用ActiveAdmin和Rails 3.1 - 有问题了解以下是否是一个错误,或者是否有某种方法正确地做到我不理解.我试图使用具有一个关系的嵌套模型,这样我就可以创建一个页面并在一步中填写它的元数据. - (page has_one meta_data,accepts_nested_attributes_for meta_data)

示例1) 在此示例中,当我单击新页面时,元数据部分存在,但没有输入字段 - 另外,如果我编辑记录,它会正确显示,但是字段集在第二部分中重复.如果我删除包含semantic_field_for的f.inputs(这将是有意义的),那么它会完全中断并在元数据区域中不显示任何内容......

form do |f|
  f.inputs "Page Information" do
    f.input :name
    f.input :uri
    f.input :view
    f.input :body, :as => :text
    f.input :active
  end

  f.inputs "Meta Data" do
    f.semantic_fields_for :meta_data do |meta_form|
      meta_form.inputs :title, :description, :keywords, :name => "Meta Information"
    end
  end  
end
Run Code Online (Sandbox Code Playgroud)

我理解元数据可能没有被实例化,但我不确定我应该如何在表单块中执行此操作?(或者,如果我甚至可以这样做) - 我能够使其工作的唯一方法是使用自定义表单,并在视图中构建元数据,如下所示

2)我是如何解决它,但似乎是hacky

<%= semantic_form_for [:admin, @page] do |f| %>
  <% @page.build_meta_data %>
  <%= f.inputs :name => "Page Information" do  %>
    <%= f.input :name %>
    <%= …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-attributes formtastic ruby-on-rails-3 activeadmin

24
推荐指数
2
解决办法
2万
查看次数

Rails嵌套属性不会触发子回调

我遇到了一个奇怪的问题,当更新父级时,不会触发子回调...

我有以下模型设置:

class Budget < ActiveRecord::Base
  has_many :line_items
  accepts_nested_attributes_for :line_items
end
Run Code Online (Sandbox Code Playgroud)

 

class LineItem < ActiveRecord::Base
  belongs_to :budget

  before_save :update_totals

  private
  def update_totals
    self.some_field = value
  end
end
Run Code Online (Sandbox Code Playgroud)

在我的表单中,我有嵌套的字段(使用构建fields_for):

= form_for @budget do |f|
  = f.text_field :name
  = f.fields_for :line_items do |ff|
    = ff.text_field :amount
Run Code Online (Sandbox Code Playgroud)

为什么update_totals孩子的回调从未被解雇/我该怎么办才能解雇?

activerecord ruby-on-rails callback nested-attributes

21
推荐指数
2
解决办法
8711
查看次数

Ruby on Rails - 嵌套属性:如何从子模型访问父模型

我有几个这样的模特

class Bill < ActiveRecord::Base
  has_many :bill_items
  belongs_to :store

  accepts_nested_attributes_for :bill_items
end

class BillItem <ActiveRecord::Base
  belongs_to :product
  belongs_to :bill

  validate :has_enough_stock

  def has_enough_stock
    stock_available = Inventory.product_is(self.product).store_is(self.bill.store).one.quantity
    errors.add(:quantity, "only #{stock_available} is available") if stock_available < self.quantity
  end
end
Run Code Online (Sandbox Code Playgroud)

上面的验证显然不起作用,因为当我从bill表单中的嵌套属性中读取bill_items时,属性bill_item.bill_id或bill_item.bill在保存之前不可用.

那我该怎么做呢?

ruby-on-rails nested-attributes

20
推荐指数
2
解决办法
7554
查看次数

使用rails嵌套模型来创建*外部对象并同时*编辑*现有的嵌套对象?

使用Rails 2.3.8

目标是创建一个Blogger,同时更新嵌套的用户模型(如果信息已更改等),或者创建一个全新的用户(如果它尚不存在).

模型:

class Blogger < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user
end
Run Code Online (Sandbox Code Playgroud)

Blogger控制器:

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
Run Code Online (Sandbox Code Playgroud)

形成:

<% form_for(@blogger) do |blogger_form| %>
  <% blogger_form.fields_for :user do |user_form| %>
    <%= user_form.label :first_name %>
    <%= user_form.text_field :first_name %>
    # ... other fields for …
Run Code Online (Sandbox Code Playgroud)

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

20
推荐指数
2
解决办法
4030
查看次数

在Rails应用程序中使用accepts_nested_attributes_for时,如何维护嵌套属性的排序

这是父模型:

class TypeWell < ActiveRecord::Base
   ...

  has_many :type_well_phases, :dependent => :destroy
  accepts_nested_attributes_for :type_well_phases, :reject_if => lambda { |a| a[:phase_id].blank? }, :allow_destroy => true

  ...
end
Run Code Online (Sandbox Code Playgroud)

这是嵌套模型:

class TypeWellPhase < ActiveRecord::Base

  belongs_to :type_well
  belongs_to :phase

end
Run Code Online (Sandbox Code Playgroud)

这是阶段模型:

class Phase < ActiveRecord::Base
  ... 
  has_many :type_well_phases
  ...
end
Run Code Online (Sandbox Code Playgroud)

我通过在父模型的控制器中复制我的阶段(阶段模型)表中的所有记录来在子表(TypeWellPhases)中添加嵌套记录,如下所示:

class TypeWellsController < ResourceController
   ...
  def new
    @new_heading = "New Type Well - Computed"
    @type_well   = TypeWell.new
    initialize_phase_fields
  end

  private

  def initialize_phase_fields
    Phase.order("id").all.each do |p|
      type_well_phase               = @type_well.type_well_phases.build
      type_well_phase.phase_id      = p.id
      type_well_phase.gw_heat_value = p.gw_heat_value …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-attributes

19
推荐指数
1
解决办法
8671
查看次数

Rails嵌套has_one:无法删除现有记录

我正在尝试在"问题"模型中更新嵌套的question_output属性.一个问题has_one question_output.如果数据库中没有现有的question_output,一切正常.但是如果记录已经有一个question_output,我在尝试更新时会得到以下结果:

无法删除现有的关联question_output.在将外键设置为nil之后,记录无法保存.

我本以为allow_destroy可以解决这个问题,但是唉 - 没有快乐.不可否认,我之前没有使用过has_one.但如果有人对如何解决这个问题有任何想法,我会很感激.相关代码如下:

表格:

= form_for [@question.project, @question], :as => :question, :url => admin_project_question_path(@question.project, @question) do |f|
  = render '/shared/form_errors', :model => @question
  = f.fields_for :question_output_attributes do |qo|
    .field
      = qo.label :question_type
      = qo.select :question_type, QuestionOutput::QUESTION_TYPES
    .field
      = qo.label :client_format
      = qo.select :client_format, QuestionOutput::CLIENT_FORMATS
    .field
      = qo.label :required
      = qo.check_box :required
    .field
      = qo.label :min_input, 'Length'
      = qo.text_field :min_length
      = qo.text_field :max_length
    = f.submit 'Save Question Formatting'
Run Code Online (Sandbox Code Playgroud)

问题模型:

class Question < ActiveRecord::Base
  has_one :question_output
  accepts_nested_attributes_for …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails has-one nested-attributes

19
推荐指数
1
解决办法
9603
查看次数

accept_nested_attributes_for的替代方案 - 也许是virtus

我对rails很新,终于找到了正确的使用方法accepts_nested_attributes_for.

不过,也有在网络上一些严重的资源,谁说,使用accepts_nested_attributes_for一般是不好的做法(这样的一个).

需要进行哪些更改以避免accepts_nested_attributes_for以及在哪个文件夹中放置其他类文件(我猜需要一个额外的类).

我读到virtus适合那个.是对的吗?

这是一个仍然使用的非常基本的例子accepts_nested_attributes_for(在这里找到完整的例子):

楷模

class Person < ActiveRecord::Base

    has_many :phones
    accepts_nested_attributes_for :phones

end

class Phone < ActiveRecord::Base

    belongs_to :person

end
Run Code Online (Sandbox Code Playgroud)

调节器

class PeopleController < ApplicationController

    def new

        @person = Person.new
        @person.phones.new

    end

    def create

        @person = Person.new(person_params)
        @person.save

        redirect_to people_path

    end

    def index

        @people = Person.all

    end

private

    def person_params

        params.require(:person).permit(:name, phones_attributes: [ :id, :number ])

    end

end
Run Code Online (Sandbox Code Playgroud)

查看(people/new.html.erb)

<%= form_for @person, do |f| …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails nested-forms nested-attributes

19
推荐指数
3
解决办法
7340
查看次数

我应该如何将rails和simple_form用于嵌套资源?

我正在尝试同时使用另一个嵌套资源创建一个资源.我正在使用Rails4和simple_form 3.0.0rc.这是我的代码.

楷模:

class User < ActiveRecord::Base
  has_one :profile
  accepts_nested_attributes_for :profile
end

class Profile < ActiveRecord::Base
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

控制器:

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
Run Code Online (Sandbox Code Playgroud)

查看(新用户的表单)

<%= simple_form_for @user do |f| %>
  <%= f.input :email %>
  <%= simple_fields_for :profile do |p| %>
    <%= p.input :name %>
  <% end %>
  <%= f.submit %>
<% …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-attributes fields-for simple-form strong-parameters

18
推荐指数
1
解决办法
2万
查看次数

Rails 4不更新嵌套属性

问题:当我点击关联的操作时,它们是在现有嵌套属性的基础上创建的,而不是更新嵌套属性#updatefeatures_controller.rb

可能的原因:我认为问题在于我在Rails中缺乏理解form_for.我认为细分在我的视图中,如何呈现持久的嵌套属性,和/或我如何指定嵌套属性的id失败,导致它只是创建一个新的id

feature.rb

class Feature < ActiveRecord::Base
  ...
  has_many :scenarios
  accepts_nested_attributes_for :scenarios,
    allow_destroy: true,
    reject_if: :all_blank
  ...
end
Run Code Online (Sandbox Code Playgroud)

features_controller.rb

def update
  ...
  project = Project.find(params[:project_id])
  @feature = Feature.find(params[:id])

  if @feature.update_attributes(feature_params)
    # checking feature_params looks good...
    # feature_params['scenarios'] => { <correct object hash> }

    redirect_to project
  else
    render :edit
  end
end

...

private
def feature_params
  params.require(:feature).permit(:title, :narrative, :price, :eta, scenarios_attributes[:description, :_destroy])
end
Run Code Online (Sandbox Code Playgroud)

_form.html.haml(简体)

= form_for [@project, @feature] do |f|
  ... …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-attributes nested-form-for ruby-on-rails-4

18
推荐指数
1
解决办法
2万
查看次数