Rails nested_form Invalid association. Make sure t

2019-08-10 21:59发布

问题:

I am using rails 4.0.4 with nested_form 0.3.2 to build an app that allows users to organize movies in lists.

I have these main models, a List model (I've excluded things such as validations):

class List < ActiveRecord::Base
  belongs_to :user
  has_many :list_movie_pairs
  has_many :movies, :through => :list_movie_pairs

  accepts_nested_attributes_for :list_movie_pairs, :allow_destroy => true
  accepts_nested_attributes_for :movies, :allow_destroy => true
end

A Movie model:

class Movie < ActiveRecord::Base
  has_many :list_movie_pairs
  has_many :lists, :through => :list_movie_pairs
  has_many :reviews
end

A ListMoviePair model for the many-to-many relationship:

class ListMoviePair < ActiveRecord::Base
  belongs_to :list
  belongs_to :movie

  validates_presence_of :list_id, :movie_id
  validates_uniqueness_of :movie_id, scope: :list_id
end

I am trying to build an interface for the user to add movies to a created list. These routes serve my purpose:

get "/users/:username/lists/:id/add" => "lists#add_movies", :as => :user_list_list_movie_pairs
post "/users/:username/lists/:id/add" => "lists#submit_movies"

These are the classes in my ListsController that should make this possible:

def add_movies
  @pair = list.list_movie_pairs.new # "list" is a helper that returns the current list
end

def submit_movies
  @list = current_user.lists.find(params[:id])
  @pair = @list.list_movie_pairs.new(pair_params)
  if @pair.save
    redirect_to user_list_path(current_user.username, @list)
  else
    render :add_movies
  end
end

def list_params
  params.require(:list).permit(:name, :description, :private, \
    list_movie_pairs_attributes: [:id, :list_id, :movie_id, :_destroy], \
    movies_attributes: [:id, :title, :_destroy])
end

And this is the form in my view

<%= nested_form_for [current_user, list, @pair] do |f| %>
  <%= f.fields_for :movies do |movie_form| %>
    <%= movie_form.text_field :title %>
    <%= movie_form.link_to_remove "Remove movie" %>
  <% end %>
  <%= f.link_to_add "Add movie", :movies %>
<% end %>

I get this error when trying to access the view:

Invalid association. Make sure that accepts_nested_attributes_for is used for :movies association.

Which pops at this line:

<%= f.link_to_add "Add movie", :movies %>

Note 1: I am using the Devise gem for users, hence the "current_user" helper;

Note 2: I have tried using both "movies" and "list_movie_pairs", i.e.:

f.fields for :list_movie_pairs

and

f.link_to_add "Add movie", :list_movie_pairs

in my view, neither association seems to work

回答1:

Your code in the view should be like this

<%= nested_form_for [current_user, list, @pair] do |f| %>
<%= f.fields_for :movies do |movie_form| %>
<%= movie_form.text_field :title %>
<%= movie_form.link_to_remove "Remove movie" %>
<%= movie_form.link_to_add "Add movie", :movies %> #note the change here
<% end %>
<% end %>

Update

There are several issues in your code

1.In your List model,this line is not required

accepts_nested_attributes_for :movies, :allow_destroy => true #not requied

2.In your ListsController,you have this line

@pair = @list.list_movie_pairs.new(pair_params)

It should be

@pair = @list.list_movie_pairs.new(list_params) because you have list_params method not pair_params