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仍然设置为默认值:大而不更新。
请大家帮忙,我一直在为自己的脑袋打个天。 我是准备在必要
感谢您的及时答复..我其实还挺新的阿贾克斯在轨道上..你可以就如何提出任何指针使用回调在rails中这样做?谢谢 – skillzAlonge
没问题。我只是编辑我的答案,有一些伪代码示例。希望你从他们那里得到一个想法。 –
非常感谢..会检查出来 – skillzAlonge