What is the difference between using ":" and "@" in fields_for

Go To StackoverFlow.com

4

I am setting up embedded forms in my rails app.

This does not work

<h1>PlayersToTeams#edit</h1>
<%= form_for @players_to_teams do |field| %>
  <%= field.fields_for @players_to_teams.player do |f| %>
    <%= f.label :IsActive %>
    <%= f.text_field :IsActive %>
  <% end %>
  <%= field.label :BT %>
  <%= field.text_field :BT %>
  <br/>
  <%= field.submit "Save", class: 'btn btn-primary' %>
<% end %> 

Gives me a ActiveRecord::AssociationTypeMismatch error. Notice the @players_to_teams.player in the forms_for line.

This does work:

<h1>PlayersToTeams#edit</h1>
<%= form_for @players_to_teams do |field| %>
    <%= field.fields_for :player do |f| %>
        <%= f.label :IsActive %>
        <%= f.text_field :IsActive %>
    <% end %>
    <%= field.label :BT %>
    <%= field.text_field :BT %>
    <br/>
    <%= field.submit "Save", class: 'btn btn-primary' %>
<% end %>

Notice the :player call in the fields_for line.

Whats the difference between using a symbol and using an instance? I would think I would want to use an instance in this case, but I guess not?

Edit

models:

class Player < ActiveRecord::Base
  has_many :players_to_teams
  has_many :teams, through: :players_to_teams
end

class PlayersToTeam < ActiveRecord::Base
  belongs_to :player
  belongs_to :team

  accepts_nested_attributes_for :player
end

controller:

class PlayersToTeamsController < ApplicationController
  def edit
    @players_to_teams=PlayersToTeam.find(params[:id])
  end

  def update
    @players_to_teams=PlayersToTeam.find(params[:id])
    respond_to do |format|
      if @players_to_teams.update_attributes(params[:players_to_team])
        format.html { redirect_to @players_to_teams, notice: 'Player_to_Team was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @players_to_teams.errors, status: :unprocessable_entity }
      end
    end
  end
end

Example Project

Github: https://github.com/workmaster2n/embedded-form-errors

2012-04-05 02:40
by Tyler DeWitt


5

It seems like fields_for can't figure out what the player association is for the @players_to_teams instance variable. You bypass that by explicitly specifying the name of the association (i.e., :player). Are you defining the association in both directions? I.e.:

class PlayersToTeam < ActiveRecord::Base
  has_one :player
end

class Player < ActiveRecord::Base
  belongs_to :players_to_team
end

Additionally, looking at the documentation for fields_for, the first parameter is called record_name, so it seems like Rails is expecting the name of the association rather than the association itself. But it's hard to tell exactly what that method does without looking further into the code, and some of their examples do indeed pass the association directly into fields_for.


Okay, I was able to clone your sample project and reproduce the error. I think I understand what's happening.

After your call to accepts_nested_attributes_for, you now have an instance method on your model named player_attributes=. This is in addition to the player= method that's normally defined for a has_one association. The player_attributes= method accepts a hash of attributes, whereas the player= method only accepts an actual Player object.

Here's an example of the text input generated when you called fields_for @players_to_teams.player:

<input name="players_to_team[player][name]" ... />

and here's that same input when calling fields_for :player:

<input name="players_to_team[player_attributes][name]" ... />

When you call update_attributes in your controller, the first example will call player=, while the second example will call player_attributes=. In both cases, the argument passed to the method is a hash (because params is ultimately just a hash of hashes).

That's why you were getting an AssociationTypeMismatch: you can't pass a hash to player=, only a Player object.

It appears that the only safe way to use fields_for with accepts_nested_attributes_for is by passing the name of the association and not the association itself.

So to answer your original question, the difference is that one works and the other doesn't :-)

2012-04-05 02:56
by Brandan
I've added my models to my question. The only thing I can see on the documentation is when they use an instance variable for the first parameter, they aren't bound inside of another form_for block - Tyler DeWitt 2012-04-05 03:01
In their first "generic example", they actually do use fields_for with an explicit association within a form_for. I was also able to create a simple app with similar models and a similar form, and it didn't generate that error. Looking at the Rails source, it seems like the only time you should receive an AssociationTypeMismatch exception is when you're assigning an object as an association and the class of the object doesn't match the class of the association. Are you doing anything like that anywhere - Brandan 2012-04-05 15:01
What do you mean by "assigning an object as an association"? I don't think I'm doing anything abnormal, but maybe I am. I've added the controller source just in case - Tyler DeWitt 2012-04-05 15:40
See the docs on AssociationTypeMismatch (got the link right this time :-). The example is assigning a Comment object to an association defined for Patch objects. Where are you seeing the exception? When generating the form (in the view) or when submitting the form (in the controller) - Brandan 2012-04-05 16:18
I just tried a sample app and am getting the same error. Maybe you could take a look at the GitHub project I linked to and see where my difference is - Tyler DeWitt 2012-04-05 16:19
Looks like we commented at the same time. I get the exception once I submit the form to the update function. It happens on the updateattributes line (as far as I can tell from the debugger). I should be assigning a player to the playersto_teams instance. That is, I don't think I'm doing something like the example in the link - Tyler DeWitt 2012-04-05 16:23
I think I've got it sorted out. I updated my answer - Brandan 2012-04-05 17:55
Ads