2016-09-06 71 views
0

我有一個麻煩,我是新的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(嵌套模型)的字段名稱,所以我認爲它從形式返回空白! –

回答

0

看來,問題出在未經許可org_images屬性在OrganizationsController。你應該添加到您的organization_parameters方法:

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

編輯:

挖掘深一點,我發現,上述方案並不總是工作後。在GitHub上的Rails回購中有關於此主題的issue。如果你想找到適合你需求的很好的解決方法,你應該閱讀它,或者查看this的答案。