Forum: Ruby on Rails How to pass field value in ajax

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8ff039c819d64a36fa14a576aa8573ad?d=identicon&s=25 Rm R. (rmehta)
on 2007-02-02 19:41
Hi,

I have a form with a field name brand as :

<input type='text' name='brand'>

now, using ajax I have to check if it is still available.

I am trying :


  <%= link_to_remote "Check if it is available",
    :update => 'avail',
    :url => 'check_brand'
      %>
Now how do I pass the value of the field 'brand' as as argument to the
'check brand' on server ?

Thanks.
Jake Parsell (Guest)
on 2007-02-02 19:51
(Received via mailing list)
i think you need the :with => '#{brand}' option.

<%= link_to_remote "Check if it is available",
   :update => 'avail',
   :url => 'check_brand',
   :with => '#{brand.id}'
     %>
Jake Parsell (Guest)
on 2007-02-02 19:57
(Received via mailing list)
or :with => 'brand = #{brand.id}'

then you can check for params[:brand] in your controller.
Phlip (Guest)
on 2007-02-02 20:00
(Received via mailing list)
Aaaaand...

Jake Parsell wrote:

> or :with => 'brand = #{brand.id}'

That becomes an URI query segment, so the = can't have spaces around it!

:-)

--
  Phlip
  http://c2.com/cgi/wiki?ZeekLand  <-- NOT a blog!!
Phlip (Guest)
on 2007-02-02 20:00
(Received via mailing list)
Put the input into a form. Then either submit the form with
form_remote_tag etc, or...

Jake Parsell wrote:

> i think you need the :with => '#{brand}' option.
>
> <%= link_to_remote "Check if it is available",
>    :update => 'avail',
>    :url => 'check_brand',
>    :with => '#{brand.id}'
>      %>

The target of :with is a JavaScript string that will insert into the
link's parameter list. The above won't work for a list of reasons.
(Sorry, Jake, but the newbs need the list, to learn what to avoid!)

Ruby won't interpret single-quoted strings '#{}' as anything but
strings containing literal hatches, braces, etc. That needed double
quotes "" to invoke the sting #{embedding} system.

Next, if brand.id existed, it would resolve at Ruby time, not at Ajax
time, after a user changes the field.

There might be some way to with=> a JavaScript string that fetches the
field value into a named parameter, but I would just go with with =>
'Form.serialize("my_form_id")'.

--
  Phlip
  http://c2.com/cgi/wiki?ZeekLand  <-- NOT a blog!!
8ff039c819d64a36fa14a576aa8573ad?d=identicon&s=25 Rm R. (rmehta)
on 2007-02-02 20:01
This is what is in controller :


  def check_brand
    brand=params['brand']
    textbrand = Textbrand.look_by_textbrand(brand)

    if textbrand.nil?
      # valid
      render :text=> 'It is available.'
    else
      render :text=> 'It is already taken.'
    end
  end

and this is in view :
  <tr>

                <td height="26" colspan="3" valign="top">
                <input onfocus="this.value='';" id='brand' maxlentgh=15
name="brand" type="text" value="(max 15 chars)" class="c11">

                </td>
                <td colspan="3"/>
              </tr>

  <tr>
                <td height="24" colspan="3" valign="top">
  <%= link_to_remote "Check if it is available",
    :update => 'avail',
    :url => 'check_brand',
    :with => 'brand=#{brand.id}'
      %>

                </td>
                <td colspan="3"/>
              </tr>
              <tr>
                <td height="3"/>
                <td colspan="5"/>
              </tr>
              <tr>
                <td id='avail' width="38%" rowspan="2" colspan="3"
class="c16"></td>
                <td height="16"/>
                <td colspan="2"/>
              </tr>

And it is not working. Any help is greatly appreciated.



Jake Parsell wrote:
> i think you need the :with => '#{brand}' option.
>
> <%= link_to_remote "Check if it is available",
>    :update => 'avail',
>    :url => 'check_brand',
>    :with => '#{brand.id}'
>      %>
8ff039c819d64a36fa14a576aa8573ad?d=identicon&s=25 Rm R. (rmehta)
on 2007-02-02 20:28
Still, none of the above ideas work. The field is in a form but I don't
need to submit the form now. Just check availability and go ahead with
the rest of the form.
8ff039c819d64a36fa14a576aa8573ad?d=identicon&s=25 Rm R. (rmehta)
on 2007-02-02 21:55
<%= link_to_remote 'check' ,
                 :update => 'avail',
                :url => {:action => 'check_brand'} ,
         :with =>"'brand=try'"
                     %>


This works with the hardcoded value of try. But how do I put the value
of field 'brand'.
Phlip (Guest)
on 2007-02-02 21:59
(Received via mailing list)
Rm Rm wrote:

> <%= link_to_remote 'check' ,
>                  :update => 'avail',
>                 :url => {:action => 'check_brand'} ,
>          :with =>"'brand=try'"
>                      %>
>
> This works with the hardcoded value of try. But how do I put the value
> of field 'brand'.

Did you read my first post?

--
  Phlip
  http://c2.com/cgi/wiki?ZeekLand  <-- NOT a blog!!
Bill Walton (Guest)
on 2007-02-02 22:06
(Received via mailing list)
Rm Rm wrote:
> The field is in a form but I don't need to
> submit the form now. Just check availability
> and go ahead with the rest of the form.

I haven't seen the controller / view code you're using to render the
form
but, based on what you've posted, I assume you're iterating through a
collection of records and rendering a table row for each one using
something
like

@brands.each do |brand|
...
end

If that's the case, then in the view code you'd use  ...

<%= link_to_remote("Check if it is available",
    :url => {:action => 'check_brand', :id => brand.id},
    :update => 'avail')   %>

and in the check_brand action you'll need to change to

brand = params[:id]

Note that this will get you to the point where the code in the
controller
gets executed.

However, the update in the browser will not happen correctly if my
assumption above about iterating through a collection is correct, given
the
code you've shown so far.  DOM elements have to have unique IDs.  It
looks
to me like each row in your table is going to have a <td> with an
id='avail'.

Another source of potential difficulty will be your use of tables for
layout.  I think the <td> can be updated, but can't remember for sure.
I've
quit using tables because of all the hacks and workarounds needed to use
them with ajax.

hth,
Bill
8ff039c819d64a36fa14a576aa8573ad?d=identicon&s=25 Rm R. (rmehta)
on 2007-02-02 22:23
Yes, I did try that. But it does not work.
8ff039c819d64a36fa14a576aa8573ad?d=identicon&s=25 Rm R. (rmehta)
on 2007-02-02 22:25
Bill,
I am not going through a loop , so update is not a problem. But I am
getting errorr brand not found .

undefined local variable or method `brand' for
#<#<Class:0x6f47eb8>:0x6f47e90>

Extracted source (around line #234):

231:
232:   <%= link_to_remote 'check' ,
233:                  :update => 'avail',
234:                  :url => {:action => 'check_brand', :id =>
brand.id}
235:
236:                        %>
237:

My view has:

 <input onfocus="this.value='';" id='brand' maxlentgh=15 name="brand"
type="text" value="(max 15 chars)" class="c11">
Phlip (Guest)
on 2007-02-02 22:32
(Received via mailing list)
Rm Rm wrote:

> 231:
> 232:   <%= link_to_remote 'check' ,
> 233:                  :update => 'avail',
> 234:                  :url => {:action => 'check_brand' }

:with => 'Form.serialize("my_form")'

> 236:                        %>

That won't submit the form, it will pass all its field values to your
action.

--
  Phlip
  http://c2.com/cgi/wiki?ZeekLand  <-- NOT a blog!!
8ff039c819d64a36fa14a576aa8573ad?d=identicon&s=25 Rm R. (rmehta)
on 2007-02-02 22:39
I don't want form to be submited, I just want to get the value of field
'brand' in  action to validate it.

Phlip wrote:
> Rm Rm wrote:
>
>> 231:
>> 232:   <%= link_to_remote 'check' ,
>> 233:                  :update => 'avail',
>> 234:                  :url => {:action => 'check_brand' }
>
> :with => 'Form.serialize("my_form")'
>
>> 236:                        %>
>
> That won't submit the form, it will pass all its field values to your
> action.
>
> --
>   Phlip
>   http://c2.com/cgi/wiki?ZeekLand  <-- NOT a blog!!
Phlip (Guest)
on 2007-02-02 22:44
(Received via mailing list)
Rm Rm wrote:

> I don't want form to be submited, I just want to get the value of field
> 'brand' in  action to validate it.

> > :with => 'Form.serialize("my_form")'

> > That won't submit the form, it will pass all its field values to your
> > action.

Do you see where I wrote "That won't submit the form"?

--
  Phlip
  http://c2.com/cgi/wiki?ZeekLand  <-- NOT a blog!!
Bill Walton (Guest)
on 2007-02-02 23:06
(Received via mailing list)
Rm Rm wrote:

> I am not going through a loop , so update is
> not a problem. But I am getting errorr brand
> not found .

I need to see the controller code you're using to 'feed' the initial
form
build to help further.  Two other things.  1) The view code that
constructs
the form.  You say this is all happening within a form, but the view
code
you posted earlier had no form or submit tags.  2) The name of the
person
with whom I'm conversing.

Best regards,
Bill
8ff039c819d64a36fa14a576aa8573ad?d=identicon&s=25 Rm R. (rmehta)
on 2007-02-03 00:08
<%= link_to_remote 'check' ,
                 :update => 'avail',
                 :url => {:action => 'check_brand'},
      :with => "'brand='+escape($F('brand'))"
 %>


This worked !! Thanks to all of you . And a big thanks to
'Ajax on Rails' by Scott Raymond which actually had an example similar
to my problem.
This topic is locked and can not be replied to.