Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the difference between using ":" and "@" in fields_for
    primarykey
    data
    text
    <p>I am setting up embedded forms in my rails app.</p> <p>This does not work</p> <pre><code>&lt;h1&gt;PlayersToTeams#edit&lt;/h1&gt; &lt;%= form_for @players_to_teams do |field| %&gt; &lt;%= field.fields_for @players_to_teams.player do |f| %&gt; &lt;%= f.label :IsActive %&gt; &lt;%= f.text_field :IsActive %&gt; &lt;% end %&gt; &lt;%= field.label :BT %&gt; &lt;%= field.text_field :BT %&gt; &lt;br/&gt; &lt;%= field.submit "Save", class: 'btn btn-primary' %&gt; &lt;% end %&gt; </code></pre> <p>Gives me a <code>ActiveRecord::AssociationTypeMismatch</code> error. Notice the <code>@players_to_teams.player</code> in the <code>forms_for</code> line.</p> <p>This does work: </p> <pre><code>&lt;h1&gt;PlayersToTeams#edit&lt;/h1&gt; &lt;%= form_for @players_to_teams do |field| %&gt; &lt;%= field.fields_for :player do |f| %&gt; &lt;%= f.label :IsActive %&gt; &lt;%= f.text_field :IsActive %&gt; &lt;% end %&gt; &lt;%= field.label :BT %&gt; &lt;%= field.text_field :BT %&gt; &lt;br/&gt; &lt;%= field.submit "Save", class: 'btn btn-primary' %&gt; &lt;% end %&gt; </code></pre> <p>Notice the <code>:player</code> call in the <code>fields_for</code> line.</p> <p>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?</p> <p><strong>Edit</strong> </p> <p>models:</p> <pre><code>class Player &lt; ActiveRecord::Base has_many :players_to_teams has_many :teams, through: :players_to_teams end class PlayersToTeam &lt; ActiveRecord::Base belongs_to :player belongs_to :team accepts_nested_attributes_for :player end </code></pre> <p>controller:</p> <pre><code>class PlayersToTeamsController &lt; 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 </code></pre> <p><strong>Example Project</strong></p> <p>Github: <a href="https://github.com/workmaster2n/embedded-form-errors" rel="nofollow">https://github.com/workmaster2n/embedded-form-errors</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload