2015-07-28 47 views
2

我一直在試圖弄清楚如何通過嵌套模型將多個圖像上傳到一個模型一段時間,現在沒有運氣。我有一個項目模型,併爲每個項目我想上傳多個圖像。我創建了一個名爲Picture的模型,並將其嵌套在Project模型中,並設置了回形針,除非上傳圖像並單擊「創建項目」,否則該圖像不會顯示在「顯示」頁面上。沒有顯示錯誤消息。請幫忙,因爲我不知道如何從這裏出發。rails回形針 - 通過嵌套模式上傳多個圖像到一個模型時遇到問題

這裏是我的代碼:

項目形式:

<%= bootstrap_nested_form_for @project, :html => {:multipart => true} do |f| %> 

<% f.fields_for :pictures do |builder| %> 
    <% if builder.object.new_record? %> 

     <p> 
     <%= builder.file_field :image %> 
     </p> 

    <% end %> 
    <%= builder.link_to_remove "Remove" %> 
    <% end %> 

    <p> 
    <%= f.link_to_add "Add Images", :pictures %> 
</p> 

<%= f.submit %> 
<% end %> 

項目控制器: -

class ProjectsController < ApplicationController 
    before_action :set_project, only: [:show, :edit, :update, :destroy] 

    respond_to :html 

    def index 
    @projects = Project.all 
    respond_with(@projects) 
    end 

    def show 
    respond_with(@project) 
    end 

    def new 
    @project = Project.new 
    @project.pictures.build 
    respond_with(@project) 
    end 


    def edit 
    @project = Project.find(params[:id]) 
    @project.pictures.build 
    end 

    def create 

    @project = Project.new(project_params) 
    if @project.save 
    flash[:notice] = "Successfully created project." 
    redirect_to @project 
    else 
    render :action => 'new' 
    end 

    end 

    def update 
    @project.update(project_params) 
    respond_with(@project) 
    end 

    def destroy 
    @project.destroy 
    respond_with(@project) 
    end 

    private 
    def set_project 
     @project = Project.find(params[:id]) 
    end 

    def project_params 
     params.require(:project).permit(:id, :title, :description, :status, :phase, :location, pictures_attributes: [:id, :image]) 
    end 
end 

項目模式: -

class Project < ActiveRecord::Base 

    has_many :pictures, :dependent => :destroy 
    accepts_nested_attributes_for :pictures, :reject_if => lambda { |t| t['picture'].nil? } 

end 

圖片型號: -

class Picture < ActiveRecord::Base 
belongs_to :project 
has_one :image 

has_attached_file :image, 
:path => ":rails_root/public/system/:attachment/:id/:style/:filename", 
:url => "/system/:attachment/:id/:style/:filename", 
:styles => { :medium => "300x300>", :thumb => "100x100>" } 

validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"] 

end 

顯示頁面: -

<% @project.pictures do |picture| %> 

    <%= image_tag picture.image_url %> 
<% end %> 

<p> 
    <strong>Title:</strong> 
    <%= @project.title %> 
</p> 

<p> 
    <strong>Description:</strong> 
    <%= @project.description %> 
</p> 

<p> 
    <strong>Status:</strong> 
    <%= @project.status %> 
</p> 

<p> 
    <strong>Phase:</strong> 
    <%= @project.phase %> 
</p> 

<p> 
    <strong>Location:</strong> 
    <%= @project.location %> 
</p> 

<%= link_to 'Edit', edit_project_path(@project) %> | 
<%= link_to 'Back', projects_path %> 

模式: -

ActiveRecord::Schema.define(version: 20150728092717) do 

create_table "pictures", force: true do |t| 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "image_file_name" 
    t.string "image_content_type" 
    t.integer "image_file_size" 
    t.datetime "image_updated_at" 
    t.integer "project_id" 
    end 

    add_index "pictures", ["project_id"], name: "index_pictures_on_project_id" 
create_table "projects", force: true do |t| 
    t.string "title" 
    t.text  "description" 
    t.string "status" 
    t.string "phase" 
    t.string "location" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 
+0

如果創建圖像,那麼你顯示它錯了。這個'<%​​= image_tag picture.image_url%>'應該是'<%= image_tag picture.image.url(:medium)%>' – Pavan

+0

試過這個但仍然沒有顯示圖像。任何想法還有什麼可能導致這個問題? –

回答

1

您的形式和白名單使用屬性名image

但是,如果他們沒有picture參數,則拒絕任何嵌套圖片。

accepts_nested_attributes_for :pictures, :reject_if => lambda { |t| t['picture'].nil? } 

嵌套屬性PARAMS不包裹在「模型鍵」之類的軌道形成PARAMS通常是這樣。這是他們的樣子:

params = { 
    project: { 
    pictures_attributes: [ 
     { 
     image: 'foo.jpg' 
     } 
    ] 
    } 
} 

你能趕上這些樣的錯誤很簡單,與模型規格:

require 'rails_helper' 
RSpec.describe Project do 
    it 'accepts nested pictures' do 
    project = Project.new(pictures_attributes: [{ image: 'foo.jpg' }]) 
    expect(project.pictures.first).to to_be_a Picture 
    end 
end 
+0

我不認爲你應該做'has_one:image'和'has_attached_file:image'。我沒有使用回形針,但它根本沒有意義,因爲回形針存儲在磁盤上,而不是存儲在ActiveRecord模型中。 – max

+0

嘿,感謝您花時間回答。我仍然不確定如何去解決這個問題。我對軌道和編碼一般都很陌生,所以我真的不知道如何使用模型規格。我應該在哪裏實施你提到的params代碼? –

+0

有什麼我應該添加/更改與我的項目控制器中的創建操作? –

相關問題