How to pass field value in ajax

Hi,

I have a form with a field name brand as :

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.

i think you need the :with => ‘#{brand}’ option.

<%= link_to_remote “Check if it is available”,
:update => ‘avail’,
:url => ‘check_brand’,
:with => ‘#{brand.id}’
%>

or :with => ‘brand = #{brand.id}’

then you can check for params[:brand] in your controller.

Aaaaand…

Jake P. wrote:

or :with => ‘brand = #{brand.id}’

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

:slight_smile:


Phlip
http://c2.com/cgi/wiki?ZeekLand ← NOT a blog!!

Put the input into a form. Then either submit the form with
form_remote_tag etc, or…

Jake P. 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!!

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.

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 :

            <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>
<%= 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”>




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

Jake P. 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}’
%>

<%= 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’.

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!!

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

with an
id=‘avail’.

Another source of potential difficulty will be your use of tables for
layout. I think the

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

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:

Yes, I did try that. But it does not work.

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!!

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!!

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!!

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

<%= 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 R. which actually had an example similar
to my problem.