Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7746331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:15:09+00:00 2026-06-01T10:15:09+00:00

I am setting up embedded forms in my rails app. This does not work

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:15:10+00:00Added an answer on June 1, 2026 at 10:15 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Setting onchange event for CheckBoxList using the following code doesn't work. chkListUserGroup.Attributes.Add(onchange, document.forms[0].isRecordModified.value='true';); How
Setting up ASP.net MVC with Linq2SQL or Entity Framework's context to have scaffolding work
Setting up the sandboxes for all these option is not feasible right now. So
Is there a setting for apache or .htaccess to not open images in browser,
I'm using Beanshell as an embedded debugging tool in my app. It means I
Moingoid doesn't seem to be setting embedded relationships persistently during my tests. In my
I’m trying to make an embedded web page on my blackberry app, and I'm
i'm trying to use htmlText on a dynamic text field with embedded fonts. i've
Setting up an integration server, I’m in doubt about the best approach regarding using
Setting up new git installations. On one Windows laptop, I'm running (under cygwin): git

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.