Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Updated 7-Oct-11 15:42pm
v2

1 solution

I fixed it myself by adding:

@order.build_user


in order_controller

def new
@order = Order.new
@order.build_user
end
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900