2015-02-11 48 views
0

我在Rails工作,我有兩個模型,一個是預先發布和一個主動。基本上我希望用戶能夠使用預啓動的屬性創建一個主動性。基本上我想要發生的事情是,當用戶訪問他們的預發佈並準備將其變成主動時,它將他們帶到已經填充了他們的發佈前信息的表單並且他們可以添加額外的信息。除了附加的圖像,我已經設法爲每個屬性執行此操作,名爲:cover_image。通過回形針上傳圖像而不保存對象

我認爲問題在於我在控制器的新操作中將預先設置的cover_image設置爲預啓動的cover_image,但由於這是新操作而不是創建,因此我還沒有保存主動性。我認爲這意味着cover_image還沒有被重新上傳,所以@ iniative.cover_image.url沒有指向任何東西。它似乎也沒有預填充我的表單的任何文件字段。

我不完全確定所有這些是否可行,但這是客戶要求的,所以我試圖讓它爲他們工作。

這裏是我的控制器:

def new 
    @initiative = Initiative.new 
    populate_defaults(@initiative) 
    @initiative.build_location 
    3.times{ @initiative.rewards.build } 
    @initiative.user = current_user 
    if !params[:prelaunch_id].nil? && !params[:prelaunch_id].empty? 
    # if user is transferring a prelaunch, assign its attributes to the intiative 
    @prelaunch = Prelaunch.find(params[:prelaunch_id]) 
    @initiative.assign_attributes(title: @prelaunch.title, 
           teaser: @prelaunch.teaser, 
           category: @prelaunch.category, 
           funding_goal: @prelaunch.funding_goal, 
           term: @prelaunch.campaign.term, 
           story: @prelaunch.story, 
           location: @prelaunch.campaign.location, 
           video_url: @prelaunch.video_url, 
           EIN: @prelaunch.campaign.EIN, 
           nonprofit: @prelaunch.nonprofit, 
           organization_name: @prelaunch.campaign.organization.name) 
    end 
end 

編輯:

由於peterept的答案在下面,我設法發射前cover_image進入形式和進入舉措控制器的創建操作。現在的問題是,創建操作中的一切看起來都很完美:主動獲得預發佈的封面圖像,並且無錯誤地保存,並重定向到顯示操作。

不幸的是,當它到達控制器的顯示動作時,@ initiative.cover_image被重新設置爲默認值。我無法弄清楚成功創建動作和演出動作之間可能會發生什麼。

這裏有舉措控制器的創建和顯示操作:

def create 
    if !params[:initiative][:prelaunch_id].nil? && !params[:initiative][:prelaunch_id].empty? 
     @prelaunch = Prelaunch.find(params[:initiative][:prelaunch_id]) # find the prelaunch if it exists 
    end 
    @initiative = Initiative.new(initiatives_params) 
    @initiative.user = current_user 
    begin 
     @payment_processor.create_account(@initiative) 
     if @initiative.save 
     # @prelaunch.destroy # destroy the prelaunch now that the user has created an initiative 
     flash[:alert] = "Your initiative will not be submitted until you review the initiative and then press 'Go Live' on the initiative page" 
     redirect_to initiative_path(@initiative) 
     else 
     flash[:alert] = "Initiative could not be saved: " + @initiative.errors.messages.to_s 
     render :new 
     end 
    rescue Exception => e 
     logger.error e.message 
     flash[:error] = "Unable to process request - #{e.message}" 
     render :new 
    end 
    end 

    def show 
    @initiative = Initiative.find(params[:id]) 
    @other_initiatives = Initiative.approved.limit(3) 
    end 

,這裏是從同一個控制器initiatives_params方法:

def initiatives_params 
    initiative_params = params.require(:initiative).permit(
    :terms_accepted, 
    :title, 
    :teaser, 
    :term, 
    :category, 
    :funding_goal, 
    :funding_type, 
    :video_url, 
    :story, 
    :cover_image, 
    :nonprofit, 
    :EIN, 
    :role, 
    :send_receipt, 
    :organization_name, 
    :crop_x, :crop_y, :crop_h, :crop_w, 
    location_attributes: [:address], 
    rewards_attributes: [:id, :name, :description, :donation, :arrival_time, :availability, :_destroy, :estimated_value]) 
    if @prelaunch.media.cover_image 
    initiative_params[:cover_image] = @prelaunch.media.cover_image 
    end 
    initiative_params 
end 

回答

0

您可以通過圖片URL,並顯示它在頁面上。

然後,用戶可以通過上傳新圖像(按照正常情況)來覆蓋該圖像。

如果你創建的動作是,如果他們沒有提供一個新的圖像,然後將其設置爲相關的預啓動之一 - 你想複製原來的,所以它不會被取代,如果他們上傳新的一個。 (如果你不知道哪個是預啓動的,你可以將ID傳遞給頁面)。