开发者

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

开发者 https://www.devze.com 2022-12-24 02:41 出处:网络
I searched and tried a lot, but I can\'t accomplish it as I want.. so here\'s my problem. My models are:

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:

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

You could do it as well in your controller new after that you create your User object.

0

精彩评论

暂无评论...
验证码 换一张
取 消