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 ?
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”)’.
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.
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.
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.