嵌套的属性没有保存在数据库Rails 5

嵌套的属性没有保存在数据库Rails 5

问题描述:

我有一个麻烦,我是新的ROR,并希望保存组织的一些图像使用嵌套属性或为了简单只是一个字符串为了尝试嵌套属性保存在数据库中,但实际上并未保存。嵌套的属性没有保存在数据库Rails 5

组织模式

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 

组织cotroller创建

def create 
@organization = Organization.new(organization_params) 

respond_to do |format| 
    if @organization.save 
    session[:organization_id][email protected] 
    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 感谢您的帮助

+0

如果你来了,你能向我们展示日志或任何错误,以便我们可以看看为什么它没有得到保存。 –

+0

不幸的是,它通常会保存组织的所有属性,但不会将嵌套的属性保存在表中(在数据库浏览器中,我可以看到组织的记录,但嵌套属性的表是空的,我会非常感激如果你刚才给我看了一个简单的例子,它使用嵌套模型正常保存。 –

+0

当我试图写recep_nested_attributes_for:stringas only not accepted_nested_attributes_for:stringas,:reject_if => lambda {| t | t ['stringa']。blank?}和填写了组织的形式,它给了我错误消息“Stringas组织必须存在”然而,我在组织形式中输入了stringas(嵌套模型)的字段名称,所以我认为它从形式返回空白! –

看来,问题出在未经许可org_images属性在OrganizationsController。你应该添加到您的organization_parameters方法:

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

编辑:

挖掘深一点,我发现,上述方案并不总是工作后。在GitHub上的Rails回购中有关于此主题的issue。如果你想找到适合你需求的很好的解决方法,你应该阅读它,或者查看this的答案。