Claudio Shigueo Watanabe
Claudio Shigueo Watanabe

Reputation: 1003

Why the code doesn't appear in the html?... in Rails

I don't know what is happening... I'm trying to send hidden_fields with has_many association but the code doesn't appear in the html.

I'm sending the code. Probably it will be more clear

In the html.erb:

  <div id="billing_fields">
     <div class="form-group col-xs-12 col-sm-11 col-md-10 col-lg-7">
       <%= f.fields_for :orders do |field| %>
       <div class="field">
           <%= field.hidden_field :digital_product_plan_id, :value => @orders.digital_product_plan_id %>
           <%= field.hidden_field :express_payer_id, :value => @orders.express_payer_id %>
           <%= field.hidden_field :express_token, :value => @orders.express_token %>
       </div>
     <% end %>
   </div>
 </div>

It is generating this html:

<div id="billing_fields">
  <div class="form-group col-xs-12 col-sm-11 col-md-10 col-lg-7">
  </div>
</div>

When I replace "f.fields_for :orders" with "f.fields_for :order" in the html.erb, this html is generated (I was expecting something like that with the "orders" (previous) version)

  <div id="billing_fields">
    <div class="form-group col-xs-12 col-sm-11 col-md-10 col-lg-7">
      <div class="field">
          <input id="user_order_digital_product_plan_id" name="user[order][digital_product_plan_id]" type="hidden" value="1" />
          <input id="user_order_express_payer_id" name="user[order][express_payer_id]" type="hidden" value="FVPYDJ7JCUY96" />
          <input id="user_order_express_token" name="user[order][express_token]" type="hidden" value="EC-32X744951C158904E" />
      </div>
  </div>
</div>

The billing_fields is inside this form_for:

<%= form_for @user, :url => registration_path(@user), html: {class: "form-horizontal", role: "form"} do |f| %>

Order controller:

def new
  @user = current_user
  @orders = Order.where(:paypal_express_token => params[:token]).last
  @orders.get_details
  if ! @orders.paypal_email.blank?
    @orders.save!
  end
  if @user.nil?
    @user = User.new
    ...
  end
  @orders.user ||= @user 
end

Order model:

belongs_to :user

User model:

attr_accessible ... :orders_attributes
has_many :orders
accepts_nested_attributes_for :orders

I'm filling lost... Any hints?

This question deals with another aspect of this problem: How to create a relationship of a new parent with an existing child in Rails

Upvotes: 1

Views: 65

Answers (1)

Richard Peck
Richard Peck

Reputation: 76784

Several issues:

f.fields_for

You probably know this, but your f.fields_for call needs to be contained within a form_for method. Your HTML shows it on its own - if this by design, you need to put it inside a form_for block


build

Your real problem is likely with a lack of calling build on your associative data:

#app/controllers/users_controller.rb
Class UsersController < ActiveRecord::Base
   def new
       @user = User.new
       @user.orders.build #-> this should get f.fields_for to show
   end
end

Here is a great resource detailing how this works

Note from SevenSeaCat:

it iterates over the user's orders outputting the fields, so it won't output fields for orders if the user doesn't have any

Upvotes: 3

Related Questions