[Facebooker-talk] facebook_form_for + a little ajax?

Mike Mangino mmangino at elevatedrails.com
Thu Sep 11 18:20:48 EDT 2008


You'll actually have to store it in a javascript variable. You can do  
something like:

In your controller:

@gift_image_hash = {}
GiftCatalog.find(:all).each {|g| @gift_image_hash[g.id] =  
g.resource.resource_url}

In your view:

<script>
   var gift_mapping = <%= @gift_image_hash.to_json %>;
</script>


Then, your function can just be:
function update_gift_display(gc_id, update_widget) {
   $(update_widget).setTextValue(gift_mapping[gc_id]);
}

You'll probably need to adjust the setTextValue call to something else  
if you want to change an image src, but that's the general idea.

Mike

On Sep 11, 2008, at 5:47 PM, Zhao Lu wrote:

> Yep I was mixing them without thinking about it!  I guess I'm getting
> confused reading on Prototype/Facebooker all at once.  I think I can
> go either way, AJax or store it somewhere.  It sounds storing it in an
> array as you have suggested is easier.  But I'll have to store it in a
> ruby variable and I have to access to it (or at least I don't know how
> to access it) from Javascript.
>
> In case I'm not being articulate enough, this is what I mean:
> I create this array in my form:
> @rs_array = GiftCatalog.find(:all, :order => "name").map { |gc|
> [gc.resource.resource_uri, gc.id]}
>
> Then in my javascript function I pass in gc.id, cause that's what's
> being selected in the drop-down.
> Now how do you get the resource_uri that's corresponding to the gc.id
> of @rs_array?
>
> Zhao
>
> On Thu, Sep 11, 2008 at 2:13 PM, Mike Mangino
> <mmangino at elevatedrails.com> wrote:
>> I think you're mixing javascript and ruby:
>>
>> uri = GiftCatalog.find(gc_id).resource.resource_uri
>>
>> That's Ruby, right?
>>
>> You'll need to either use ajax to get that data or store it in the  
>> page
>> somewhere. Maybe you could keep an array that maps between gc_id  
>> values and
>> their URLs.
>>
>> I often will prototype my ajax stuff outside of Facebook. It's  
>> easier to
>> debug that way. Once it's working I convert it to Facebook.
>>
>> Mike
>> On Sep 11, 2008, at 3:06 PM, Zhao Lu wrote:
>>
>>> Thanks for your answer Mike.  I was calling select_tag by itself
>>> without the wrapping,
>>> which is indeed what I need to do.  Now I have passed that having  
>>> some
>>> problem with
>>> getting hold of a ruby class I defined from javascript.  I can't  
>>> seem
>>> to find an example
>>> online.  Here's my code:
>>>
>>> <p id="resource" >&nbsp;</p>
>>> <% facebook_form_for :facebook_gift, FacebookGift.new, :url =>
>>> facebook_gifts_path do |f| %>
>>> <%=
>>> @gift_catalog = GiftCatalog.find(:all, :order => "name").map { |gc|
>>> [gc.resource.file_name, gc.id]}
>>> f.text select_tag(:gift_catalog_id,
>>> options_for_select(@gift_catalog), :onchange =>
>>> "update_gift_display(this.getValue(), 'resource');")
>>> %>
>>> //more code here
>>> <%end%>
>>>
>>> I have some javascript on the same page:
>>>
>>> <script>
>>> function update_gift_display(gc_id, update_widget) {
>>>  uri = GiftCatalog.find(gc_id).resource.resource_uri
>>>  $(update_widget).setTextValue(uri)
>>> }
>>> </script>
>>>
>>> So what I'm trying to achieve is for user to select a  
>>> gift_catalog_id,
>>> then I will get the url to
>>> the actual gift (which is linked to gift_catalog).  But  
>>> GiftCatalog is
>>> not available in the javascript function.
>>> I feel that I'm not on the right track for this.  Any suggestion  
>>> would
>>> be greatly appreciated.
>>>
>>> Zhao
>>>
>>> On Thu, Sep 11, 2008 at 5:30 AM, Mike Mangino
>>> <mmangino at elevatedrails.com> wrote:
>>>>
>>>> Are you calling form.select_tag? That won't work. select_tag is  
>>>> not a
>>>> method
>>>> on the form builder (this is true any time you use form_for in  
>>>> rails)
>>>>
>>>> You can do form.text select_tag... to wrap a normal select inside a
>>>> facebook
>>>> form.
>>>>
>>>> Mike
>>>> On Sep 11, 2008, at 1:45 AM, Zhao Lu wrote:
>>>>
>>>>> Hello Everyone,
>>>>>
>>>>> I'm using a facebook_form_for which is bound to a model.  In the  
>>>>> form
>>>>> I have a form.select which is populated with an array of the  
>>>>> names of
>>>>> images.
>>>>> Whenever I change the selection I'd like to display the image on  
>>>>> the
>>>>> same page.  I tried to change form.select to select_tag but that
>>>>> doesn't seem to play well with facebook_form_for.  I'm pretty  
>>>>> confused
>>>>> as to how to approach this.  Any suggestion would be appreciated.
>>>>>
>>>>> --
>>>>> Zhao
>>>>> _______________________________________________
>>>>> Facebooker-talk mailing list
>>>>> Facebooker-talk at rubyforge.org
>>>>> http://rubyforge.org/mailman/listinfo/facebooker-talk
>>>>
>>>> --
>>>> Mike Mangino
>>>> http://www.elevatedrails.com
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> Zhao
>>
>> --
>> Mike Mangino
>> http://www.elevatedrails.com
>>
>>
>>
>>
>
>
>
> -- 
> Zhao

--
Mike Mangino
http://www.elevatedrails.com





More information about the Facebooker-talk mailing list