简体   繁体   English

将关联(has_one)模型中的字段与Rails一起使用

[英]Using fields from an association (has_one) model with formtastic in rails

I searched and tried a lot, but I can't accomplish it as I want.. so here's my problem. 我进行了很多搜索和尝试,但是我无法按要求完成它。所以这是我的问题。

My models are: 我的模型是:

class User < ActiveRecord::Base
  has_one :profile
  accepts_nested_attributes_for :profile
end

class Profile < ActiveRecord::Base
  attr_accessible :user_id, :form, :title, :name, :surname, :street, :housenumber, :zipcode, :place, :phone, :mobile, :fax, :url 
  belongs_to :user
end

In my view: 在我看来:

<% semantic_form_for @user do |form| %>
  <%= form.inputs :login, :email, :password%>
  <% form.semantic_fields_for :profile do |profile| %>
    <%= profile.inputs %>
  <% end %>
  <%= form.buttons %>  
<% end %>

My problem is that when I edit a person then it shows me the data on the profile. 我的问题是,当我编辑一个人时,它会向我显示个人资料上的数据。 I would, that the fields from the profile even when creating a user are displayed. 我希望即使在创建用户时也会显示配置文件中的字段。

Thanks a lot! 非常感谢!

You should add in your view before your form.semantic_fields_for: 您应在表单之前添加视图。semantic_fields_for:

<% @user.build_profile unless @user.profile %>

You could do it as well in your controller new after that you create your User object. 创建User对象之后,也可以在新的控制器中完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM