[Rails] AJAX image manipulation

Dion Hewson dionhewson at gmail.com
Wed Aug 9 13:06:21 GMT 2006


there are a couple of ways, dont take my code verbatim, its just to get you
thinking in the right direction

one is to update a div using ajax i.e.
 "onclick"=>" new Effect.Fade('blank_div'); new Effect.Appear('preview_div')"


or just render a partial with the img

an ajax way is to use an *.rjs file (the rails/ruby javascript ) to update a
div on your page with the selected thumbnail

e.g in your view
--------------------------------------------------

<div id="div_list_edit<%= @pic.id %>">

<img src="bob.jpg" onclick="do_preview" >

</div>

<div id="preview"></div>

Controller
----------------------------------------------------
def do_preview
   p_id = #{params[:id]}
   @pic = Product.find(:first, :conditions => ["id = ? ", p_id ])
  end

do_preview.rjs
----------------------------------------------------
page.delay(1) do
  #page.show 'div_list' + @params[:id].to_s  #gold
  page.insert_html :after, 'div_list_edit' + @params[:id].to_s, :partial =>
'preview'

end

_preview.rhtml
--------------------

<img src="<%= @pic.image_url %>">            #img_tag will look nicer


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


OR you could simply do this, without all the ajaxie hoopla

Controller
----------------------------------------------------
def do_preview
   p_id = #{params[:id]}
   @pic = Product.find(:first, :conditions => ["id = ? ", p_id ])
   render(:partial => "preview")
  end

like I said this is just ideas not an actual working example

cheers

dion

www.blogsaic.com

On 8/9/06, Jim Weir <javawaba at hotmail.com> wrote:
>
> I have this code in a controller that returns images to my browser...with
> ROR.
>
>   def index
>     @products = Product.find_all_ pictures
>   end
>
> ....this is the .rhtml..
>
>
> <% for photo in @pic -%>
>   <div class="entry">
>     <img src="<%= photo.image_url %>"/>
>     <h3><%= h(photo.title) %></h3>
>     <%= photo.description %>
>   </div>
> <% end %>
>
> ....can someonw show me how I can return the results to AJAX?  What I want
> to
> do is to display  thumbnails along the bottom and when a user clicks on
> one
> have it a 5x7 area on the screen populated...
>
>   Can AJAX receive resultsets from ROR?
>
> Thanks,
> Jim
>
> _________________________________________________________________
> Express yourself instantly with MSN Messenger! Download today - it's FREE!
> http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
>
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060809/a463e1c0/attachment.html


More information about the Rails mailing list