Rails 3 中的字段未显示内部显示元素

发布于 2024-11-17 06:17:12 字数 1154 浏览 3 评论 0原文

我有一个名为 Order 的模型和另一个名为 Member 的模型,当我尝试在 Orders 视图中显示 Members 模型中的字段时,使用 fields_for 标记时它甚至不会显示。这是我的代码的样子。

订单模型

class Order < ActiveRecord::Base
  has_many :members 
end

成员模型

class Member < ActiveRecord::Base
  belongs_to :order
end

订单控制器

class OrdersController < ApplicationController

  def new
    @order = Order.new
    3.times { @order.members.build }
    @title = "Order Form"
  end

  def create
    @order = Order.new params[:order]
    if @order.save 
      flash[:notice] = "Your order has been created"
      redirect_to orders_path
    else
      @title = "Order Form"
      render 'new'
    end
  end 
end

问题出在我的订单视图中:

<% for member in @order.members %>
  This displays 3 times but the information below doesn't
  <% fields_for "...", member do |member_form| %>
    <p>
      Name: <%= member_form.text_field :name %>
    </p>
  <% end %>
<% end %>

由于某些奇怪的原因,标签字段中的信息甚至不会显示一次。我错过了什么吗?

如果您发现我做错了什么,请向我解释一下,因为我是 Rails 新手。

提前致谢!

I have a model named Order and another model named Member and when I try to display fields from the Members model in my Orders view it doesn't even show when using the fields_for tag. Heres what my code looks like.

order model

class Order < ActiveRecord::Base
  has_many :members 
end

member model

class Member < ActiveRecord::Base
  belongs_to :order
end

orders controller

class OrdersController < ApplicationController

  def new
    @order = Order.new
    3.times { @order.members.build }
    @title = "Order Form"
  end

  def create
    @order = Order.new params[:order]
    if @order.save 
      flash[:notice] = "Your order has been created"
      redirect_to orders_path
    else
      @title = "Order Form"
      render 'new'
    end
  end 
end

The issue is in my orders view:

<% for member in @order.members %>
  This displays 3 times but the information below doesn't
  <% fields_for "...", member do |member_form| %>
    <p>
      Name: <%= member_form.text_field :name %>
    </p>
  <% end %>
<% end %>

For some odd reason the information in the fields for tag won't even display once. Am I missing something?

If you find out what I am doing wrong, can you please explain it to me because I am new to rails.

Thanks in advance!

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

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

发布评论

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

评论(2

幸福丶如此 2024-11-24 06:17:12

为集合中的 fields_for 调用提供的块将针对集合中的每个实例重复,本质上创建其自己的循环,因此您实际上不需要编写自己的显式循环“for member in @order.members”。此外,您可以利用nested_attributes功能来直接使用@order.save保存关联成员:

class Order < ActiveRecord::Base
  has_many :members 
  accepts_nested_attributes_for :members
end

class Member < ActiveRecord::Base
  belongs_to :order
end

在视图中:

<%= form_for @order do |order_form| %>
  ...
  <%= order_form.fields_for :members do |member_form| %>
    Name: <%= member_form.text_field :name %>
  <% end %>
<% end %>

我认为您的控制器创建方法应该像您拥有的那样工作。

请参阅 fields_for 的 API 文档,尤其是一对多小节。

The block given to a fields_for call on a collection will be repeated for each instance in the collection, essentially creating its own loop, so you don't really need to write your own explicit loop "for member in @order.members". Further, you can leverage nested_attributes functionality to enable saving of associated members directly with @order.save:

class Order < ActiveRecord::Base
  has_many :members 
  accepts_nested_attributes_for :members
end

class Member < ActiveRecord::Base
  belongs_to :order
end

In the view:

<%= form_for @order do |order_form| %>
  ...
  <%= order_form.fields_for :members do |member_form| %>
    Name: <%= member_form.text_field :name %>
  <% end %>
<% end %>

And I think your controller create method should work as you have it.

See the API docs for fields_for, especially the One-to-many subsection.

星軌x 2024-11-24 06:17:12

我认为您只需要在对 fields_for 的调用中删除 "...", ,因为 fields for 需要一个对象。

尝试:

<% fields_for member do |member_form| %>

I think you just need to get rid of the "...", in your call to fields_for, as fields for is expecting an object.

Try:

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