accepts_nested_attributes_for +属于_不显示字段 [英] accepts_nested_attributes_for + belongs_to does not show fields
本文介绍了accepts_nested_attributes_for +属于_不显示字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个模型:
I have two models:
class User < ActiveRecord::Base
acts_as_authentic
has_many :orders
end
和
and
class Order < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
end
这个想法是,如果用户已经登录,我只想为该用户下订单,但是如果他没有登录,那么我将要求他在下订单的同时进行注册.这是订单的形式:
The idea is that if the user is already logged in, I just want to make an order for that user, but if he is not logged in, then I will ask him to register in the same time as making an order. Here is the form for orders:
<%= form_for(@order) do |f| %>
<div class="field">
<%= f.label :quantity %><br />
<%= f.number_field :quantity %>
</div>
<% if current_user %>
<div class="field">
<%= current_user.email%>
</div>
<%else%>
<%= f.fields_for(:user) do |user_form| %>
<%= render "newuser_fields", :f => user_form %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
和新用户字段是:
and new users fields are:
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
问题在于它不会为用户显示字段!
有什么建议吗?
The problem is that it won''t show fields for user!
Any suggestions?
推荐答案
我自己添加了以下内容进行修复:
I fixed it myself by adding:
@order.build_user
在order_controller
in order_controller
def new
@order = Order.new
@order.build_user
end
这篇关于accepts_nested_attributes_for +属于_不显示字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文