Rails 3 中的字段未显示内部显示元素
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为集合中的 fields_for 调用提供的块将针对集合中的每个实例重复,本质上创建其自己的循环,因此您实际上不需要编写自己的显式循环“for member in @order.members”。此外,您可以利用nested_attributes功能来直接使用@order.save保存关联成员:
在视图中:
我认为您的控制器创建方法应该像您拥有的那样工作。
请参阅 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:
In the view:
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.
我认为您只需要在对
fields_for
的调用中删除"...",
,因为 fields for 需要一个对象。尝试:
I think you just need to get rid of the
"...",
in your call tofields_for
, as fields for is expecting an object.Try: