嵌套模型形式错误

发布于 2024-10-01 01:15:13 字数 1654 浏览 5 评论 0原文

我有一个嵌套模型表单,它抛出错误“#的未定义方法'media_type'”当调用 update_attributes 时。 media_type 关联有什么问题?

class Publication < ActiveRecord::Base
  has_many :products
  accepts_nested_attributes_for :products, :allow_destroy => true
end

class Product < Offering
  belongs_to :media_type
end

class Offering < ActiveRecord::Base
  belongs_to :publication
end

class MediaType < ActiveRecord::Base
  belongs_to :meaning
  has_many :products
end

这是我要提交给表格的内容。

{"commit"=>"Commit changes",
 "_method"=>"put",
 "authenticity_token"=>"e2/62ffmRVuNsCVP65zy4SLprWgRSa+DdLc2RXzM+UQ=",
 "id"=>"628",
 "publisher_publication"=>{"edition_attributes"=>{"title"=>"this is the title",
 "short_description"=>"this is the description",
 "abstract"=>"",
 "subtitle"=>"",
 "id"=>"200",
 "long_description"=>"",
 "title_prefix"=>"",
 "work_attributes"=>{"id"=>"200"}},
 "volume"=>"",
 "issue"=>"",
 "date_published"=>"2006-09-20",
 "products_attributes"=>{"1289147822429"=>{"price"=>0,
 "document"=>#<File:/var/folders/e9/e965IrazFgu0fm-rjRtvIk+++TI/-Tmp-/RackMultipart20101107-638-1vffwzk-0>,
 "media_type_id"=>"1"}},
 "imprint_id"=>"3"}}

这是我的控制器操作。

def update
  @publication = Publisher::Publication.find(params[:id])

  if @publication.update_attributes(params[:publisher_publication])
    flash[:notice] = "Successfully updated publication and products."
    redirect_to(publisher_publication_url(@publication))
  else
    render :action => 'edit'
  end
end

I have a nested model form that throws the error "undefined method 'media_type' for #<Array:0x1060460d0>" when calling update_attributes. What is wrong with the media_type association?

class Publication < ActiveRecord::Base
  has_many :products
  accepts_nested_attributes_for :products, :allow_destroy => true
end

class Product < Offering
  belongs_to :media_type
end

class Offering < ActiveRecord::Base
  belongs_to :publication
end

class MediaType < ActiveRecord::Base
  belongs_to :meaning
  has_many :products
end

Here is what I am submitting to the form.

{"commit"=>"Commit changes",
 "_method"=>"put",
 "authenticity_token"=>"e2/62ffmRVuNsCVP65zy4SLprWgRSa+DdLc2RXzM+UQ=",
 "id"=>"628",
 "publisher_publication"=>{"edition_attributes"=>{"title"=>"this is the title",
 "short_description"=>"this is the description",
 "abstract"=>"",
 "subtitle"=>"",
 "id"=>"200",
 "long_description"=>"",
 "title_prefix"=>"",
 "work_attributes"=>{"id"=>"200"}},
 "volume"=>"",
 "issue"=>"",
 "date_published"=>"2006-09-20",
 "products_attributes"=>{"1289147822429"=>{"price"=>0,
 "document"=>#<File:/var/folders/e9/e965IrazFgu0fm-rjRtvIk+++TI/-Tmp-/RackMultipart20101107-638-1vffwzk-0>,
 "media_type_id"=>"1"}},
 "imprint_id"=>"3"}}

Here is my controller action.

def update
  @publication = Publisher::Publication.find(params[:id])

  if @publication.update_attributes(params[:publisher_publication])
    flash[:notice] = "Successfully updated publication and products."
    redirect_to(publisher_publication_url(@publication))
  else
    render :action => 'edit'
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衾梦 2024-10-08 01:15:13

我刚刚解决了类似问题通过使用 rails console 进行故障排除...

也许这也会对您有所帮助。

I just solved a similar problem by troubleshooting it with rails console...

Maybe this will help you too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文