【问题标题】:not able to save belongs_to association record which is nested无法保存嵌套的 belongs_to 关联记录
【发布时间】:2012-08-04 09:24:52
【问题描述】:

我有一个典型的模型控制器关系,其中包含许多嵌套形式。

以下是详细信息:-

article.rb :-

belongs_to :author
accepts_nested_attributes_for :author

作者.rb :-

attr_accessible :first_name, :last_name
has_many :articles

项目.rb

has_many :work_pairs
  has_many :source_articles, :through => :work_pairs

accepts_nested_attributes_for :work_pairs
  accepts_nested_attributes_for :source_articles

projects/new.html.slim

= semantic_form_for @project, :html => { :id => 'project_form' } do |form|
  = form.inputs do
    = form.input :user_id, :as => :hidden, :value => current_user.id

    = form.semantic_fields_for :source_articles do |article|
      = article.input :name_article, :label => "Name of the Article"

      = article.semantic_fields_for :author do |author|
        = author.input :first_name
        = author.input :last_name

projects_controller.rb

def new
    return unless require_login
    new! do
      @title = t('projects.new.title')
      @project.rewards.build
      @project.work_pairs.build
      @project.source_articles.build
      @project.source_articles.first.build_author
      @project.source_articles.first.build_publisher
    end
  end

但是,当我的作者记录没有保存在数据库中时,它也不会显示任何错误。

当我尝试通过这样做从控制台访问作者时:-

p = Project.first
p.source_articles.first.author

我得到 nil 作为输出,有人可以告诉我是什么问题吗?

【问题讨论】:

    标签: ruby-on-rails-3 ruby-on-rails-3.1 associations nested-forms


    【解决方案1】:

    您可以通过以下方式检查错误

    p.errors.messages
    

    我敢打赌,有一个验证没有通过。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多