Ruby on rails &引用;接受嵌套属性“;实际上不接受模型中的属性

Ruby on rails &引用;接受嵌套属性“;实际上不接受模型中的属性,ruby-on-rails,rails-activerecord,Ruby On Rails,Rails Activerecord,我正在做一个项目,其中有一些任务组成了一个清道夫狩猎。当用户创建新的搜索时,我希望hunts/show.html.erb文件显示搜索以及与该搜索相关的任务。但是这些模型给我带来了麻烦。我已经设置了hunt模型,它接受任务模型的嵌套属性。因此,当用户创建一个新的搜索时,她也会自动创建三个任务。我可以保存新的狩猎任务,但无法保存这些新任务。这是我的模型 少了什么?在HunTasks.rb文件中是否需要“attr accessible”语句 class Hunt < ActiveRecord::

我正在做一个项目,其中有一些任务组成了一个清道夫狩猎。当用户创建新的搜索时,我希望hunts/show.html.erb文件显示搜索以及与该搜索相关的任务。但是这些模型给我带来了麻烦。我已经设置了hunt模型,它接受任务模型的嵌套属性。因此,当用户创建一个新的搜索时,她也会自动创建三个任务。我可以保存新的狩猎任务,但无法保存这些新任务。这是我的模型

少了什么?在HunTasks.rb文件中是否需要“attr accessible”语句

class Hunt < ActiveRecord::Base

  has_many :hunt_tasks
  has_many :tasks, :through => :hunt_tasks
  accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
  attr_accessible :name
  validates :name,  :presence => true,
                    :length   => { :maximum => 50 } ,
                    :uniqueness => { :case_sensitive => false }

end


class Task < ActiveRecord::Base

  has_many :hunt_tasks
  has_many :hunts, :through => :hunt_tasks
  attr_accessible :name    
  validates :name,  :presence => true,
                    :length   => { :maximum => 50 } ,
                    :uniqueness => { :case_sensitive => false }   
end


class HuntTask < ActiveRecord::Base     
  belongs_to :hunt # the id for the association is in this table
  belongs_to :task
end
class-Hunt:搜索任务
接受_嵌套的_属性_for:tasks,:reject_if=>lambda{a | a[:content].blank?},:allow_destroy=>true
可访问属性:名称
验证:name,:presence=>true,
:length=>{:max=>50},
:唯一性=>{:区分大小写=>false}
结束
类任务:狩猎任务
可访问属性:名称
验证:name,:presence=>true,
:length=>{:max=>50},
:唯一性=>{:区分大小写=>false}
结束
类任务
以下是我的狩猎控制器的外观:

class HuntsController < ApplicationController

  def index
     @title = "All Hunts"
     @hunts = Hunt.paginate(:page => params[:page])
  end

  def show
    @hunt = Hunt.find(params[:id])
    @title = @hunt.name 
    @tasks = @hunt.tasks.paginate(:page => params[:page])
  end

  def new
    if current_user?(nil) then    
      redirect_to signin_path
    else
      @hunt = Hunt.new
      @title = "New Hunt"
      3.times do
        hunt =  @hunt.tasks.build   
      end
    end
  end

  def create
    @hunt = Hunt.new(params[:hunt])
    if @hunt.save
      flash[:success] = "Hunt created!"
      redirect_to hunts_path
    else
      @title = "New Hunt"
      render 'new'     
    end
  end
....
end
class HuntsControllerparams[:page])
结束
def秀
@hunt=hunt.find(参数[:id])
@title=@hunt.name
@tasks=@hunt.tasks.paginate(:page=>params[:page])
结束
def新
如果当前用户?(无),则
重定向到登录路径
其他的
@亨特
@title=“新狩猎”
3.5倍
hunt=@hunt.tasks.build
结束
结束
结束
def创建
@hunt=hunt.new(参数[:hunt])
如果@hunt.save
flash[:success]=“已创建狩猎!”
重定向到搜索路径
其他的
@title=“新狩猎”
呈现“新”
结束
结束
....
结束

您的示例与railscast的主要区别在于,您所做的是多对多,而不是一对多(我认为他的was调查有很多问题)。根据您所描述的,我想知道HuntTask模型是否必要。一次狩猎的任务是否会在另一次狩猎中恢复?假设他们是,那么看起来你的答案如下:

要执行此操作,您必须在控制器中修改新操作:

hunt =  @hunt.hunt_tasks.build.build_task
然后,您需要更改狩猎模型以包括:

accepts_nested_attributes_for :hunt_tasks
accepts_nested_attribues_for :hunt
并修改任务模型以包括:

accepts_nested_attributes_for :hunt_tasks
accepts_nested_attribues_for :hunt

嘿,本;)只是检查一下,您运行了任务迁移,对吗?另外,请出示相关的控制器代码,thx,Michael。顺便说一句,这个railscast非常熟悉这个:您好!很高兴再次见到你!回答你的问题,是的,我已经运行了迁移。另外,我刚刚在帖子的主要部分发布了狩猎控制器。是的,我在Railscast 196上工作,但是贝茨先生让这看起来很容易,而我发现这是一个漫长而艰难的过程。我一直在玩这个设置,但没有太大的成功。我总是在HuntsController中出现关联错误
ArgumentError#新未找到名称
hunt_任务的关联。`只是为了确认:在狩猎模型中,“接受:狩猎任务的嵌套属性”行被放置在“has_many:hunt_任务”和“has_many:tasks,through…”之后?嗯,嗯,从代码上看,只有在接受hunt中的:hunt\u任务的\u嵌套的\u属性\u之前没有声明过多个:hunt\u任务时,才会出现这种情况。我不知道还能给你指什么。也许数据库的设置方式与模型不一致?也就是说,没有同时引用“hunt”和“tasks”的“hunt_tasks”表?我仔细检查了一下,以防万一,但这里是我编写和运行的迁移创建表格“狩猎任务”:force=>true do | t | t.integer“狩猎id”t.integer“任务id”t.datetime“创建于”t.datetime“更新于”end `我遇到了上述类似的问题。