I am very new to ruby, and trying to get auto complete / predictive text
working, but I am getting the error "undefined local variable or method
`providers’ "
Here is the first box in my view
<label for="">Provider:</label>
<div>
<%= text_field_with_auto_complete :orders, :provider,
{ :tabindex => 1,:size => 30, :maxlength => 100 },
{ :url => { :controller => :orders,
:action => :auto_complete_for_provider_name},
:indicator => 'ajax-prov-load-ind',
:after_update_element =>
"function(element,value){$('activation_customer').enable();$('activation_customer').focus();}"
} -%>
<span style="display:none;" id="ajax-prov-load-ind"><%= image_tag
'ajax-load-ind.gif', :style => 'vertical-align:middle;' %></span> <a
href="#TB_inline?height=350&width=400&inlineId=add_provider&modal=true"
class="thickbox">Add Provider</a>
</div>[/code]
And here is the auto_complete_for_provider_name.rhtml, with the bold
line where it throws the error.
<ul>
[b]<% providers.each do |key, value| -%>[/b]
<li id="<%=h value -%>"><%=h key -%></li>
<% end -%>
</ul>
here is a snippit from orders controller,
def auto_complete_for_provider_name
@provider = params[:orders][:provider].downcase
@providers = Provider.call_provider_list_service(@provider)
render :partial => 'auto_complete_for_provider_name', :locals =>
{:providers => @providers}
end
end
I am just lost. Can anyone tell me why I would get a undefined method
undefined method `provider’ for #Order:0x48ff74c
Extracted source (around line #16):
13:
14: Provider:
15:
16: <%= text_field_with_auto_complete :order, :provider,
17: { :tabindex => 1,:size => 30, :maxlength => 100
},
18: { :url => { :controller => :orders,
19: :action =>
:auto_complete_for_provider_name},
Here is the declaration of my orders controller
def auto_complete_for_provider_name
@provider = params[:order][:provider].downcase
@providers = Provider.call_provider_list_service(@provider)
render :partial => ‘auto_complete_for_provider_name’, :locals =>
{:providers => @providers}
end
If you are wanting something with the same functionality that is not
designed around a specific object, you can use text_field_tag along with
auto_complete_field.
How is your Order class setup? Does it have a provider method? In other
words, does this return true in script/console:
Order.new.respond_to? provider
William P. wrote:
How is your Order class setup? Does it have a provider method? In other
words, does this return true in script/console:
Order.new.respond_to? provider
I am not sure how to run that command. I tried it from dos prompt in
project dir home doing this and didn’t seem to execute anything
script:console Order.new.respond_to? provider
I have changed the view to this:
<%= text_field_with_auto_complete :orders, :provider, {
:tabindex => 1,:size => 30, :maxlength => 100 },
{ :url => { :controller => :orders,
:action => :auto_complete_for_provider_name},
and the orders_controller.rb has this:
def auto_complete_for_order_provider
@provider = params[:order][:provider].downcase
@providers = Provider.call_provider_list_service(@provider)
render :partial => ‘auto_complete_for_provider_name’, :locals =>
{:providers => @providers}
end
and auto_complete_for_provider_name.rhtml has this:
<% providers.each do |key, value| -%> <<<<<<<<<<< NEW ERROR HERE
- <%=h key -%>
<% end -%>
bringing me back to the error undefined local variable or method
`providers’
Isn’t providers declared right there in the orders_controller.rb though?
Ok, here is the basic deal. The code you are using requires your Order
object to have a provider method. This would be either a column in the
table, an association, or possible a custom method. Based on the method
you are using for autocomplete, you don’t want to use
text_field_with_auto_complete, you need to use a combination of
text_field_tag and auto_complete_field. You can read more about them
here:
http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptMacrosHelper.html#M000576
William P. wrote:
If you are wanting something with the same functionality that is not
designed around a specific object, you can use text_field_tag along with
auto_complete_field.
This is working code in another page that I inherited to try and learn
Ruby. I just started with a new object, harvesting the important code
changing the method names and appropriate variables.
William P. wrote:
Ok, here is the basic deal. The code you are using requires your Order
object to have a provider method. This would be either a column in the
table, an association, or possible a custom method. Based on the method
you are using for autocomplete, you don’t want to use
text_field_with_auto_complete, you need to use a combination of
text_field_tag and auto_complete_field. You can read more about them
here:
ActionView::Helpers::FormTagHelper
http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptMacrosHelper.html#M000576
Wonderful! Thank you. I didn’t know that a column in the table could be
considered a method as well. That clears up so much. Thanks again!
On 10/12/07, Hallik P. [email protected] wrote:
I am not sure how to run that command. I tried it from dos prompt in
project dir home doing this and didn’t seem to execute anything
script:console Order.new.respond_to? provider
You do it like this:
C:\myproject>ruby script/console
The “>>” is the console prompt. Now you can enter Ruby expressions in
the context of your Rails environment:
Order.new.respond_to? :provider
=> false
(note that respond_to? takes a symbol, so you need “:provider”, not
“provider”)
Learn the console. Love it. :~)
HTH
That was my bad. I should have explained the console better. Btw,
respond_to? will attempt to convert it’s argument to a symbol, so
anything responding to to_sym should work:
User.new.respond_to? ‘login’
=> true
User.new.respond_to? :login
=> true
-Bill
Bob S. wrote:
C:\myproject>ruby script/console
(note that respond_to? takes a symbol, so you need “:provider”, not “provider”)
Learn the console. Love it. :~)
HTH
–
Sincerely,
William P.
On 10/12/07, William P. [email protected] wrote:
That was my bad. I should have explained the console better. Btw,
respond_to? will attempt to convert it’s argument to a symbol, so anything
responding to to_sym should work:
User.new.respond_to? ‘login’
=> true
User.new.respond_to? :login
=> true
Right, but
User.new.respond_to? login
isn’t correct, and that’s what he was using.
Very true. I missed that. Have a good weekend.
Bob S. wrote:
Right, but
User.new.respond_to? login
isn’t correct, and that’s what he was using.
–
Sincerely,
William P.