2014-05-22 45 views
0

我有一個form_for有一個fields_for雙重對象窗體創建。第二個對象是具有回形針(紅寶石寶石)屬性的附件,用於添加歸因於第一個對象的照片。盒子模型正在創建,但附件模型並不像我所知道的那麼遠。我的問題之一是multipart =>真正需要去的地方,form_for,fields_for還是兩者?放在哪裏:html => {:multipart => true}在form_for&fields_for中?

編輯:似乎box_id不能作爲參數傳遞,因爲該框尚未創建。那麼如何將附件歸於控制器中的盒子呢?我以爲

@box.attachments.build(params) 

會做出正確的歸因,但這是行不通的。

我需要用戶嵌套屬性嗎?

這裏是控制檯日誌:

Processing by BoxesController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"0vka+nZDc56OdISMctWoJjY8CH+TR20PHBl8oVglD5A=", "box"=>{"user_id"=>"45", "name"=>"asdfasdf", "origin"=>"asdfasdf", "description"=>"asdfasdf"}, "attachment"=>{"box_id"=>"", "screen_shot"=>#<ActionDispatch::Http::UploadedFile:0x007fc3d8202990 @tempfile=#<Tempfile:/tmp/RackMultipart20140521-11119-37nj3s>, @original_filename="P5110017.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"attachment[screen_shot]\"; filename=\"P5110017.JPG\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Share the Love!"} 

注意,box_id是空白的,但在模型驗證我沒有得到任何錯誤。

這裏是我的表單代碼:

<%= form_for @box, :html => { :multipart => true } do |f| %> 
    <%= f.hidden_field :user_id, :value => current_user.id %> 

    <%= f.label :name, "Name" %><br> 
    <%= f.text_field :name, autofocus: true %><br> 

    <%= f.label :origin, 'Website' %><br> 
    <%= f.text_field :origin %><br> 

    <%= f.label :description %><br> 
    <%= f.text_area :description, cols: "30", rows: "4" %> 

    <%= fields_for @attachment do |ff| %> 
     <%= ff.hidden_field :box_id, :value => @box.id %> 
     <%= ff.file_field :screen_shot, id:"attachment", :required => true %> 
    <% end %> 

    <%= f.submit "Create!" %> 
    <% end %> 

這裏是我的控制器:

class BoxesController < ApplicationController 

    def new 
    @box = Box.new 
    @attachment = Attachment.new 
    end 

    def create 
    @box = current_user.boxes.build(boxes_params) 
    @attachment = @box.attachments.build(attachment_params) 
    if @box.save 
     flash[:success] = "Box created" 
     redirect_to @box 
    else 
     flash[:error] = "There was an error" 
     redirect_to 'new' 
    end 
    end 

    def update 
    @box = Box.find(params[:id]) 
    if @box.update_attributes(boxes_params) 
     flash[:success] = "Box updated" 
     redirect_to @box 
    else 
     render 'edit' 
    end 
    end 

    def destroy 
    @box = Box.find(params[:id]) 
    @box.destroy 
    flash[:success] = "Box deleted." 
    redirect_to root_url 
    end 

    private 

    def boxes_params 
     params.require(:box).permit(:description, :name, :origin, :attachment) 
    end 

    def attachment_params 
     params.require(:attachment).permit(:screen_shot, :box_id) 
    end 
end 

和盒子模型:

class Box < ActiveRecord::Base 
    belongs_to :user 
    has_many :comments 
    has_many :attachments, :dependent => :destroy, :limit => 4 
    is_impressionable 
    validate :attachments_count_within_limit, :on => :create 
    validates :user_id, presence: true 
    validates_presence_of :name 
    validates_length_of :description, :maximum => 1000 

附件型號:

class Attachment < ActiveRecord::Base 
    belongs_to :box 
    has_attached_file :screen_shot, :styles => { :medium => "300x300>", :thumb => "100x100>" } 
    validates_attachment_content_type :screen_shot, :content_type => /\Aimage\/.*\Z/ 
    validates :box_id, presence: true 
end 

我還曾試圖通過

@attachment = Attachment.new(attachment_params) 

改變box_controller創建附件但仍然沒有奏效。

+0

box_id一起嘗試在你的視圖打印出@ box.id,看看它是否包含了所需ID。 – Thalaivar

回答

0

嘗試打印出你的觀點@box.id,看看它是否包含了所需的id,也把你與:screenshot

def attachment_params 
    params.require(:attachment).permit(:box_id, :screen_shot) 
end 
+0

好吧,我一直在嘗試不同的事情,我最初確實有:box_id在attachment_params中。否則,我認爲你是正確的道路。 box_id在新頁面上不存在,因爲該框尚未創建。因此,它不能作爲參數傳遞。 – hodale

相關問題