field_for 和带有 mongoid 的嵌套形式

发布于 2024-10-18 08:13:47 字数 358 浏览 1 评论 0原文

有人能给我使用 mongoid 的嵌套表单的工作示例吗?

我的模型:

class Employee 
  include Mongoid::Document 
  field :first_name 
  field :last_name 
  embeds_one :address 
end

class Address 
  include Mongoid::Document 
  field :street 
  field :city 
  field :state 
  field :post_code 
  embedded_in :employee, :inverse_of => :address 
end

Could somebody give me the working example of nested form using mongoid?

My models:

class Employee 
  include Mongoid::Document 
  field :first_name 
  field :last_name 
  embeds_one :address 
end

class Address 
  include Mongoid::Document 
  field :street 
  field :city 
  field :state 
  field :post_code 
  embedded_in :employee, :inverse_of => :address 
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玉环 2024-10-25 08:13:47

您的模型:

class Employee 
  include Mongoid::Document 

  field :first_name 
  field :last_name 
  embeds_one :address
  # validate embedded document with parent document
  validates_associated :address
  # allows you to give f.e. Employee.new a nested hash with attributes for
  # the embedded address object
  # Employee.new({ :first_name => "First Name", :address => { :street => "Street" } })
  accepts_nested_attributes_for :address
end

class Address 
  include Mongoid::Document 

  field :street 
  field :city 
  field :state 
  field :post_code 
  embedded_in :employee, :inverse_of => :address 
end

您的控制器:

class EmployeesController < ApplicationController

  def new
    @employee = Employee.new
    # pre-build address for nested form builder
    @employee.build_address
  end

  def create
    # this will also build the embedded address object 
    # with the nested address parameters
    @employee = Employee.new params[:employee]

    if @employee.save
      # [..]
    end
  end      

end

您的模板:

# new.html.erb
<%= form_for @employee do |f| %>
  <!-- [..] -->
  <%= f.fields_for :address  do |builder| %>
     <table>
       <tr>
         <td><%= builder.label :street %></td>
         <td><%= builder.text_field :street %></td>
       </tr>
       <!-- [..] -->
     </table>
  <% end %>
<% end %>

这应该适合您!

朱利安

Your models:

class Employee 
  include Mongoid::Document 

  field :first_name 
  field :last_name 
  embeds_one :address
  # validate embedded document with parent document
  validates_associated :address
  # allows you to give f.e. Employee.new a nested hash with attributes for
  # the embedded address object
  # Employee.new({ :first_name => "First Name", :address => { :street => "Street" } })
  accepts_nested_attributes_for :address
end

class Address 
  include Mongoid::Document 

  field :street 
  field :city 
  field :state 
  field :post_code 
  embedded_in :employee, :inverse_of => :address 
end

Your controller:

class EmployeesController < ApplicationController

  def new
    @employee = Employee.new
    # pre-build address for nested form builder
    @employee.build_address
  end

  def create
    # this will also build the embedded address object 
    # with the nested address parameters
    @employee = Employee.new params[:employee]

    if @employee.save
      # [..]
    end
  end      

end

Your template:

# new.html.erb
<%= form_for @employee do |f| %>
  <!-- [..] -->
  <%= f.fields_for :address  do |builder| %>
     <table>
       <tr>
         <td><%= builder.label :street %></td>
         <td><%= builder.text_field :street %></td>
       </tr>
       <!-- [..] -->
     </table>
  <% end %>
<% end %>

That should work for you!

Julian

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文