标签: nested-attributes

使用嵌套模型的未定义方法`build_users'

我遇到了嵌套属性的问题.

这是我的账户模型:

class Account < ActiveRecord::Base

    has_many :products
    has_many :blogs
    has_many :openings
    has_many :users
    has_one :logo, :class_name => "AccountPicture"
    has_one :address, :class_name => "AccountAddress"
    has_and_belongs_to_many :options


    accepts_nested_attributes_for :logo, :allow_destroy => true
    accepts_nested_attributes_for :address, :allow_destroy => true
    accepts_nested_attributes_for :users, :allow_destroy => true

end
Run Code Online (Sandbox Code Playgroud)

这是我的用户模型:

class User < ActiveRecord::Base
    belongs_to :account
end
Run Code Online (Sandbox Code Playgroud)

如您所见,Account接受徽标,地址和用户的嵌套属性.

在测试时,我可以使用嵌套属性来标识和地址,但不能用于用户.

a = Account.new

=> #<Account id: nil, hostname: nil, subdomain: nil, name: nil, description: nil, base_line: nil, footer: nil, phone_number: nil, mobile_number: nil, email_address: nil, created_at: nil, updated_at: …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-forms nested-attributes

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

什么是从Ruby中的对象数组中提取嵌套对象数组的禁区方法?>

我有一个Elements数组,每个元素都有一个属性:image.

我想要一系列:图像,所以最快和最便宜的方式来实现这一目标.它只是迭代数组并将每个元素推送到一个新数组,如下所示:

images = []
elements.each {|element| images << element.image}
Run Code Online (Sandbox Code Playgroud)

ruby arrays iterator nested-sets nested-attributes

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

Rails:在嵌套模型表单中使用accepts_nested_attributes_for

在我的应用中,用户有很多对话,而对话有很多消息.我想创建一个新的对话:我必须指定用户(读者)和(第一个)消息.我试过以下,但失败了.

楷模

class Conversation < ActiveRecord::Base

  has_many :conversation_users
  has_many :users, :through => :conversation_users

  has_many :messages

  accepts_nested_attributes_for :users
  accepts_nested_attributes_for :messages

end

class Message < ActiveRecord::Base

  belongs_to :conversation
  belongs_to :user

end

class User < ActiveRecord::Base

  has_many :conversation_users
  has_many :conversations, :through => :conversation_users

end
Run Code Online (Sandbox Code Playgroud)

调节器

def new
  @conversation = Conversation.new
  2.times do
    users = @conversation.users.build
  end
  messages = @conversation.messages.build
end

def create
  @conversation = Conversation.new(params[:conversation])

  if @conversation.save
    redirect_to username_conversations_path(current_username)
  else
    redirect_to new_username_conversation_path(current_username)
  end
end
Run Code Online (Sandbox Code Playgroud)

视图

<% form_for([current_user, @conversation]) do |f| %>

   <% f.fields_for …
Run Code Online (Sandbox Code Playgroud)

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

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

如何在rails中限制模型的嵌套属性数

我有两个相关的模型:Group和Member.

Group.rb:

has_many :members, :dependent => :destroy
accepts_nested_attributes_for :members, :reject_if => lambda { |a| a[:email].blank? and a[:id].blank? }, :allow_destroy => true
Run Code Online (Sandbox Code Playgroud)

我想要做的是添加一个验证,以防止member_count达到25后立即添加成员.

因此,如果我编辑一个组,请举例说:

  1. 我有20个现有成员
  2. 我在浏览器端添加了8个来自FORM的成员

它应保存前5个记录,然后引发错误,例如"您已超出嵌套属性的限制".

在rails中是否有任何内置方法来执行此操作.作为一个比较新手的铁路我不知道这个?

validation limit nested-attributes ruby-on-rails-3

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

如何用rails中的json更新nested_attributes?

我有

 class Test < ActiveRecord::Base
        has_many :samples
        accepts_nested_attributes_for :samples
 end
Run Code Online (Sandbox Code Playgroud)

 class Sample < ActiveRecord::Base
    belongs_to :tests
 end
Run Code Online (Sandbox Code Playgroud)

现在,当我创建新值如下

curl -H'Content-Type:application/json'-H'Eccept:application/json'-X POST http://localhost:3000/tests.json-d"{\"test \":{\"name \":\"xxxxxx \","samples_attributes\":[{\"用户名\ ":\" YYYYYYY\"\ "年龄\":\ "25 \"}]}}"

它为测试创建一个新值,它还在sample表中创建一个条目,但在更新时更新测试表,而在样本表中创建一个新条目.那我怎么能让它更新目前的条目?

请帮我.

ruby-on-rails nested-attributes ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

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

如何使rails strong_parameters + nested_attributes + serialize工作?

我很难让rails 4使用nested_attributes和serialize.我有:

class Client < ActiveRecord::Base
  belongs_to :event
  serialize :phones
end


class Event < ActiveRecord::Base
  has_one :client
end


class EventsController < ApplicationController

  ...

  def event_params
    params.permit(client_attributes: [:phones])
  end
end
Run Code Online (Sandbox Code Playgroud)

当我通过活动时:

{client_attributes: { phones: 'string'}}
Run Code Online (Sandbox Code Playgroud)

它有效,但当我尝试

{client_attributes: { phones: [{phone_1_hash},{phone_2_hash}]}}
Run Code Online (Sandbox Code Playgroud)

我收到'未经许可的参数:手机'消息并且字段未保存...

我试过用

class EventsController < ApplicationController

  ...

  def event_params
    params.permit(client_attributes: [phones:[]])
  end
end
Run Code Online (Sandbox Code Playgroud)

要么

class Client < ActiveRecord::Base
  belongs_to :event
  serialize :phones, Array
end
Run Code Online (Sandbox Code Playgroud)

但到目前为止没有任何帮助 任何建议,将不胜感激.谢谢!

serialization nested-attributes strong-parameters ruby-on-rails-4

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

使用accepts_nested_attributes_for打开未允许的参数

所以我对Rails相当新,所以我可能会错过一些非常直接的东西..

我要做的是在创建Artist时创建一个Band.

楷模

band.rb

class Band < ActiveRecord::Base

  has_many :artists, dependent: :destroy

  accepts_nested_attributes_for :artists, allow_destroy: true

  ...

end
Run Code Online (Sandbox Code Playgroud)

artist.rb

class Artist < ActiveRecord::Base

  belongs_to :band

  ...

end
Run Code Online (Sandbox Code Playgroud)

控制器 band_controller.rb

class BandController < ApplicationController

  def new
    @band = Band.new
  end

  def create

    @band = Band.new(band_params)
    if @band.save
      ...
    else
      ...
    end

  end

  private

    def band_params
      params.require(:band).permit(:name, :hometown, :email, :artist, artist_attributes: [ :band_id, :first_name, :last_name, :email, :password, :password_confirmation ])
    end

end
Run Code Online (Sandbox Code Playgroud)

视图

new.html.erb

<%= form_for(@band, url: "/artist/signup") do …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails nested-attributes ruby-on-rails-4

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

Rails 4带有fields_for的嵌套属性不保存到数据库

我想使用accepts_nested_attributes_for通过一个表单在两个不同的表(场地和停车场)上创建记录.我希望用户能够创建一个新的场地,并通过复选框指定该场地可用的停车选项.当我提交表单时,会创建包含模型(场所)的记录,但嵌套模型(停放)没有任何反应.当我检查来自服务器的响应时,我发现我遇到了"未经许可的参数:parking_attributes",虽然我不知道为什么.

我已经看过Railscast#196嵌套模型表,并尝试了多个stackoverflow帖子的建议(Rails 4嵌套属性不保存,Rails 4:fields_for in fields_for,Rails 4 - 嵌套模型(2级)不保存).如果有人可以帮助我,我会非常感激.

我已经包括了两个模型,场地控制器,场地/新视图以及服务器的响应.

venue.rb

class Venue < ActiveRecord::Base
  has_many :parkings
  accepts_nested_attributes_for :parkings
end
Run Code Online (Sandbox Code Playgroud)

parking.rb

class Parking < ActiveRecord::Base
  belongs_to :venue
end
Run Code Online (Sandbox Code Playgroud)

venues_controller.rb

class VenuesController < ApplicationController
  def index
    @venues = Venue.all
  end

  def new
    @venue = Venue.new
  end

  def create
    @venue = Venue.new(venue_params)
    if @venue.save
      redirect_to @venue, flash: { success: "Venue successfully created" }
    else 
      render :new
    end
  end

  def show
    @venue = Venue.find(params[:id])
  end …
Run Code Online (Sandbox Code Playgroud)

checkbox nested-attributes fields-for strong-parameters ruby-on-rails-4

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

Ruby on Rails回形针具有嵌套属性的多个上传

我想知道是否有人可以帮助我上传文件!

我正在尝试使用回形针上传多个图像并具有嵌套属性.

楷模

class Trip < ActiveRecord::Base
  has_many :trip_images, :dependent => :destroy
end

class TripImage < ActiveRecord::Base
  belongs_to :trip
  has_attached_file :photo, :styles => { :large => "800x800>", :medium => "500x500>", :thumb => "150x150#" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :photo, content_type: /\Aimage\/.*\Z/
end
Run Code Online (Sandbox Code Playgroud)

调节器

def create
  @trip = Trip.new(trip_params)
  respond_to do |format|
    if @trip.save
      format.html { redirect_to @trip, notice: 'Trip was successfully created.' }
      format.json { render :show, status: :created, location: @trip }
    else
      format.html { render :new }
      format.json { …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails paperclip nested-attributes ruby-on-rails-4

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

Rails: Conditional nested attributes in edit form

I have a model called Offer and another called PurchasinGroup

Offer has many PurchasingGroups

Offer accepts nested attributes for PurchasingGroups

While creating an offer you can add as many PurchasingGroups as you want.

PurchasingGroup has a boolean attribute called active.

while editing an Offer you can see all the created PurchasingGroups, however I want to let the user edit only the PurchasingGroups that are active, and do not display the inactive purchasing groups.

This is my edit action in …

nested-attributes ruby-on-rails-4

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