【问题标题】:The nested attributes is not saved in the database on Rails 5嵌套属性未保存在 Rails 5 的数据库中
【发布时间】:2016-09-06 09:56:57
【问题描述】:

我遇到了一个麻烦,我是 ROR 的新手,我想使用嵌套属性为组织保存一些图像,或者为了简单起见只是一个字符串,以便尝试将嵌套属性保存在数据库中,但实际上它没有保存。

组织模式

class Organization < ApplicationRecord
has_secure_password
has_many :needs, dependent: :destroy
has_many :org_images, dependent: :destroy
has_many :stringas, dependent: :destroy
accepts_nested_attributes_for :org_images, :reject_if => lambda { |t| t['org_image'].blank? }
accepts_nested_attributes_for :stringas, :reject_if => lambda { |t| t['stringa'].blank? }

架构

create_table "org_images", force: :cascade do |t|
t.string   "caption"
t.integer  "organization_id"
t.datetime "created_at",         null: false
t.datetime "updated_at",         null: false
t.string   "photo_file_name"
t.string   "photo_content_type"
t.integer  "photo_file_size"
t.datetime "photo_updated_at"
end

  create_table "stringas", force: :cascade do |t|
t.string   "name"
t.datetime "created_at",      null: false
t.datetime "updated_at",      null: false
t.integer  "organization_id"
end

组织主管

def new
@organization = Organization.new
3.times {@organization.org_images.build}
@organization.stringas.build # added this
end
  def organization_params
 params.require(:organization).permit(:org_name, :email, :password, :info,:image, :website_URL, :contacts, :logo_url , :password_confirmation  ,stringas_attributes:[:name,:id,:organization_id,:created_at,:updated_at] ,org_images_attributes: [:id,:organization_id,:caption, :photo_file_name, :photo_content_type,:photo_file_size,:photo_updated_at])

结束 组织形式

<div class= "field">
<% if builder.object.new_record? %>

  <p>
  <%= builder.label :caption, "Image Caption" %>
  <%= builder.text_field :caption %>
  </p>

  <p>
  <%= builder.label :photo, "Image File" %>
  <%= builder.file_field :photo %>
  </p>

<% end %>
<% if builder.object.new_record? %>

  <p>
  <%= builder.label :name %>
  <%= builder.text_field :name%>
  </p>

<% end %>

<% end %>

Stringa 和 org_image 模型

class OrgImage < ApplicationRecord
belongs_to :organization
has_attached_file :photo, :styles => { :small => "150x150>", :large =>      "320x240>" }
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
end

class Stringa < ApplicationRecord
belongs_to :organization
end

组织控制器创建

  def create
@organization = Organization.new(organization_params)

respond_to do |format|
  if @organization.save
    session[:organization_id]=@organization.id
    format.html { redirect_to @organization, notice: 'Organization was successfully created.' }
    format.json { render :show, status: :created, location: @organization }

  else
    format.html { render :new }
    format.json { render json: @organization.errors, status: :unprocessable_entity }
  end
end

结束 git repository 感谢您的帮助

【问题讨论】:

  • 你能告诉我们日志或任何错误,以便我们看看为什么它没有被保存。
  • 不幸的是,它会正常保存组织及其所有属性,但不会将嵌套的属性保存在其表中(在数据库浏览器中,我看到了组织的记录,但嵌套属性的表是空,如果你给我看一个使用正常保存的嵌套模型的简单示例,我将非常感谢。
  • 当我尝试写accepts_nested_attributes_for :stringas 时只不accepts_nested_attributes_for :stringas, :reject_if => lambda { |t| t['stringa'].blank?并再次填写了组织的表格,它给了我错误消息“Stringas 组织必须存在”但是我在组织表单中输入了 stringas(嵌套模型)的字段名称,所以我认为它从表单中返回为空白!!

标签: ruby-on-rails database


【解决方案1】:

问题似乎在于您的OrganizationsController 中未经允许的org_images 属性。您应该将此添加到您的 organization_parameters 方法中:

params.require(:organization).permit( ... , org_images_attributes: [:photo, :caption])

编辑:

深入挖掘后,我发现上述解决方案并不总是有效。在 GitHub 上的 Rails 存储库中有一个关于此主题的 issue。如果您想找到适合您需求的好解决方法,您应该阅读它,或查看this 答案。

【讨论】:

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