2013-12-16 87 views
0

我正在使用cloudinary gem在rails中的照片共享應用程序。 在我#picasController我有以下相關行動如何設置返回變量在ajax調用Ruby on Rails

def filter 
    @pica = Pica.find(params[:id]) 
    @new_upload = current_user.picas.first 
    effect_id = params[:effect_id] 
    @effect = effect_id.to_sym 
    respond_to do |format| 
     format.html { render 'filter' } 
     format.js 
    end 
    end  

    def share 
    @pica = Pica.find(params[:id]) 
    @edited_photo_url = params[:edited_photo] 

    @new_photo_public_id = get_new_public_id(@edited_photo_url) 
    @pica.update_attributes(photo: @new_photo_public_id, user_id: current_user) 
    flash.now[:success] = "Saving Photo: Successful" 
    @new_pica = current_user.picas.first 

    end 

    def edit 
    @pica = Pica.find(params[:id]) 
@new_upload = current_user.picas.first 
    end 


//views/edit.html.erb 
<% render 'shared/filter' %> 

    //partial 
    /shared/_filter.html.erb 

    <h2>Select an effect to apply below:</h2> 
     <div id="image-display"> 

     <%= cl_image_tag(@new_upload.photo_url(@effect)) %> 
     <% active_effect = :large %> 

     </div> 

     <%= link_to "Remove", @pica, class: "btn btn-warning btn-small", method: :delete,data: { confirm: "Are You sure?" } %><br/>  

    </div> 

    <div class="span2 share-photo-btn"><br/><br/><br/><br/><br/><br/>  


     <%= active_effect %>  
    <% edited = @new_upload.photo_url(active_effect) %>  
    <%= edited %>  
     <%= link_to "Save and Share", {:controller => :picas, :action => :share, :edited_photo => edited, id: @pica.id}, class: "btn btn-info btn-large" %><br/>  

</div> 

    </div><br/> 
    <div class="effects"> 
    <ul class="thumbnails filters"> 
    <li> 
    <span class="E_title">Sepia</span> 
    <% thumb_id = "sepia" %> 
    <%= link_to image_tag(@new_upload.photo_url(:sepia)), {:controller => :picas, :action => :filter, id: @pica.id, effect_id: thumb_id}, :data => {:remote=> true}, class: "thumbnail", id: "sepia" %> 
    </li>  

    <li>  
    <span class="E_title">Grayscale</span> 
     <% thumb_id = "grayscale" %> 
    <%= link_to image_tag(@new_upload.photo_url(:grayscale)), {:controller => :picas, :action => :filter, id: @pica.id, effect_id: thumb_id}, :data => {:remote=> true}, class: "thumbnail", id: "grayscale" %> 
     </li> 

     <li> 
     <span class="E_title">Blackwhite</span> 
     <% thumb_id = "blackwhite" %> 
     <%= link_to image_tag(@new_upload.photo_url(:blackwhite)),  {:controller => :picas, :action => :filter, id: @pica.id, effect_id: thumb_id}, :data => {:remote=> true}, class: "thumbnail", id: "blackwhite" %> 
    </li>  


//filter.js.erb 

<% return_effect = "original" %>  


<% case @effect  
     when :sepia 
      return_effect = 'sepia'  
     when :grayscale  
      return_effect = 'grayscale'  
     when :blackwhite  
      return_effect = 'blackwhite'  
     when :gradient_fade 
      return_effect = 'gradient_fade'  
     when :negate 
      return_effect = 'negate'  
     when :vignette 
      return_effect = 'vignette'  
     else 
    return_effect 
    end 
%> 


//this loads a partial depending on the value of 'return_effect' 
    $("#image-display").html('<%= escape_javascript(render("#{return_effect}")).html_safe %>') 

請我的問題是有在過濾器部分設置爲正確的值active_effect變量每當用戶點擊的效果。我試過,包括它被點擊的效果,但它doesent工作這樣當加載部分之中:

當棕褐色效果鏈接用戶點擊:用JS加載
部分是這樣的:

//_sepia.html.erb 
    <%= cl_image_tag(@new_upload.photo_url(:sepia_canvas)) %> 
<% active_effect = :sepia_canvas %> 

    <br/> 

但仍然無法正常工作。
我所需要的是每當單擊鏈接時更新active_effect變量的值。
active_effect仍然設置爲默認值:大而不更新。
請大家幫忙,我一直在爲自己的腦袋打個天。 我是準備在必要

回答

0

對於異步任務提供額外的信息,最好的做法是不返回任何值(如果你這樣做,大部分時間它沒有返回值),相反,如果你想操縱任何數據(來自異步響應),通過回調,處理回調中的數據。

不要:

def async_task 
    do_sth 
    return response 
end 

務必:

def async_task(&blk) 
    do_sth 
    blk.call(response) 
end 
+0

感謝您的及時答覆..我其實還挺新的阿賈克斯在軌道上..你可以就如何提出任何指針使用回調在rails中這樣做?謝謝 – skillzAlonge

+0

沒問題。我只是編輯我的答案,有一些僞代碼示例。希望你從他們那裏得到一個想法。 –

+0

非常感謝..會檢查出來 – skillzAlonge