【问题标题】:Rails nested_form Invalid association. Make sure that accepts_nested_attributes_for is used for assoctiationRails nested_form 无效关联。确保accepts_nested_attributes_for 用于关联
【发布时间】:2014-07-15 02:47:58
【问题描述】:

我正在使用带有 nested_form 0.3.2 的 rails 4.0.4 来构建一个允许用户在列表中组织电影的应用程序。

我有这些主要模型,一个列表模型(我已经排除了诸如验证之类的东西):

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

电影模型:

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

用于多对多关系的 ListMoviePair 模型:

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

我正在尝试为用户构建一个界面,以将电影添加到创建的列表中。这些路线符合我的目的:

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

这些是我的 ListsController 中应该使这成为可能的类:

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

这是我认为的形式

<%= 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 %>

尝试访问视图时出现此错误:

无效的关联。确保accepts_nested_attributes_for 用于:movies 关联。

在这一行弹出:

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

注意 1:我正在为用户使用 Devise gem,因此是“current_user”助手;

注2:我尝试过同时使用“movies”和“list_movie_pairs”,即:

:list_movie_pairs 的 f.fields

f.link_to_add "添加电影", :list_movie_pairs

在我看来,这两个协会似乎都不起作用

【问题讨论】:

  • 尝试将您的这一行 &lt;%= nested_form_for [current_user, list, @pair] do |f| %&gt; 更改为 &lt;%= nested_form_for [@list, @pair] do |f| %&gt;
  • 在这一行@pair = @list.list_movie_pairs.new(pair_params) 你写了pair_params 实际方法名是list_params
  • @Pavan 更改 nested_form_for 行将不起作用,因为它需要不同的路线(即 list_list_movie_pair_path)。此外,pair_params 是另一个专门用于 ListMoviePair 对象的助手。然而,错误不可能源自那里,因为控制器无法在没有用户单击提交的情况下点击 submit_movie 方法 - 这不会发生,因为无法显示 add_movie 视图
  • 你为什么在list params 方法中有一个backslash(\)
  • 您是否尝试从List 模型中评论此行accepts_nested_attributes_for :movies, :allow_destroy =&gt; true

标签: ruby-on-rails-4 associations nested-form-for


【解决方案1】:

你在视图中的代码应该是这样的

<%= 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 %>

更新

您的代码中有几个问题

1.在您的List 模型中,此行不是必需的

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

2.在你的ListsController,你有这行

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

应该是

@pair = @list.list_movie_pairs.new(list_params) 因为你有list_params 方法而不是pair_params

【讨论】:

  • 我已经相应地更改了我的代码,但它仍然不起作用,我得到了同样的“无效关联”错误
  • 我也会在这里写这个:我不相信 pair_params 调用会导致这种情况,因为控制器永远不会调用提交方法,因为它不能首先显示 add_movie 视图(注意:调用 list_params反而不起作用,大概是因为调用者不是List对象)
  • @razvan9310 尝试在 List 模型中评论这一行 accepts_nested_attributes_for :movies, :allow_destroy =&gt; true
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 1970-01-01
  • 2010-10-30
  • 1970-01-01
相关资源
最近更新 更多