Rails nested_form无效的关联.确保使用accepts_nested_attributes_for进行联合 [英] Rails nested_form Invalid association. Make sure that accepts_nested_attributes_for is used for assoctiation

查看:110
本文介绍了Rails nested_form无效的关联.确保使用accepts_nested_attributes_for进行联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我有以下主要模型,即List模型(我已经排除了诸如验证之类的东西):

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

电影模型:

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

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

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"

这些是我的ListsController中的类,应该可以实现以下目的:

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

这是我认为的形式

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

无效的关联.确保:movies关联使用了accepts_nested_attributes_for.

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

在此行弹出:

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


注1:我正在为用户使用Devise gem,因此使用了"current_user"帮助程序;


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

注2:我尝试同时使用电影"和"list_movie_pairs",即:

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

:list_movie_pairs的f.fields

f.fields for :list_movie_pairs

f.link_to_add添加电影":list_movie_pairs

f.link_to_add "Add movie", :list_movie_pairs

在我看来,这两个关联似乎都不起作用

in my view, neither association seems to work

推荐答案

您在视图中的代码应像这样

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

更新

您的代码中有几个问题

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

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

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

2.在您的ListsController中,您有这行

2.In your ListsController,you have this line

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

应该是

@pair = @list.list_movie_pairs.new(list_params),因为您使用的是list_params方法而不是pair_params

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

这篇关于Rails nested_form无效的关联.确保使用accepts_nested_attributes_for进行联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆