When using alias_attribute of a model in fields_for doesn't generate the correct form #1887

Closed
velles opened this Issue Jun 28, 2011 · 4 comments

5 participants

@velles

If a alias for a table is declared
EX:

class PrivateMessage < ActiveRecord::Base
  has_many :private_messages_recipients
  alias_attribute  :pm_recipients, :private_messages_recipients
end 

class PrivateMessagesRecipient < ActiveRecord::Base
  belongs_to :private_message
end

In the controller:

  def new
    @private_message = PrivateMessage.new
    @private_message.pm_recipients .build
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @private_message }
    end
  end

And then generate the form:

    <%= f.fields_for :pm_recipients do |pmr_f|  %>
        <%= pmr_f.hidden_field :id %>
        <%= pmr_f.label :recipient_id %><br />
        <%= pmr_f.text_field :recipient_id %>
    <% end %>

It does not generate properly but when <%= f.fields_for :private_messages_recipients do |pmr_f| %> is used it works fine

@bhus

Looking at the docs (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for) it seems like you are supposed to either do :

<%= f.fields_for @private_message.pm_recipients do |pmr_f| %>

or

<%= f.fields_for :private_messages_recipients do |pmr_f| %>

no?

@isaacsanders

@velles Is this still an issue?

@frodsan

I'm trying to reproduce it with master.

When :private_message_recipients is used, it generates this:

<%= form_for @private_message do |f| %>
  <% f.fields_for :private_message_recipients do |pm_f| %>
    <%= pm_f.hidden_field :id %>
    <%= pm_f.label :message %>
    <%= pm_f.text_field :message %>
  <% end %>
<% end %>
<form accept-charset="UTF-8" action="/private_messages" class="new_private_message" id="new_private_message" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value=""><input name="authenticity_token" type="hidden" value="ZsnqCMokHf3BQIIeL3BvaxaikVHf3nAgkJb/L2W4jrE="></div>    <input id="private_message_private_message_recipients_id" name="private_message[private_message_recipients][id]" type="hidden">
    <label for="private_message_private_message_recipients_message">Message</label>
    <input id="private_message_private_message_recipients_message" name="private_message[private_message_recipients][message]" type="text">
</form>

And when :pm_recipients is used:

<%= form_for @private_message do |f| %>
  <% f.fields_for :pm_recipients do |pm_f| %>
    <%= pm_f.hidden_field :id %>
    <%= pm_f.label :message %>
    <%= pm_f.text_field :message %>
  <% end %>
<% end %>
<form accept-charset="UTF-8" action="/private_messages" class="new_private_message" id="new_private_message" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="ZsnqCMokHf3BQIIeL3BvaxaikVHf3nAgkJb/L2W4jrE=" /></div>    <input id="private_message_pm_recipients_id" name="private_message[pm_recipients][id]" type="hidden" />
    <label for="private_message_pm_recipients_message">Message</label>
    <input id="private_message_pm_recipients_message" name="private_message[pm_recipients][message]" type="text" />
</form>

I don't have a heavy background in views, but could you explain what is it not generated properly?

@steveklabnik
Ruby on Rails member

Yeah, that all looks right to me. @velles can you explain what you think it should be? I'm gonna give this a close for now, as it's been two months since the @isaacsanders bump, and we can't reproduce an issue. If you can provide an example of why it's wrong, we can re-open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment