Monday, 2 September 2013

Field with auto_complete

Field with auto_complete

newbe question here.
I've inherited a rails project and been asked to change some chunk of
code, but I have a very little experience in Ruby and Rails (I am an
objective-c with a PHP background developper)
So the first stuff I have to had is a textfield with an auto complete
mode. Actually my code has this code for the auto completed text field :
registration_controller.rb
def auto_complete_for_training_title
puts "auto_complete_for_training_title"
current_locale = params[:locale].to_s
if current_locale == 'en' then
if current_user.member then
Locale.set current_user.member.written_language
else
Locale.set 'fr'
end
end
@trainings = Training.find(:all, :conditions => ["((title LIKE ? OR fso_id
LIKE ?) and is_private = 0", '%' + params[:training][:title] + '%', '%' +
params[:training][:title] + '%'])
puts @trainings.count
render :partial => 'complete_ajax_own_function_training'
end
In the view, the text field is created like this :
<% form_for :sortRegistration, { :url => { :action => 'index'} } do |f| %>
<p><label><%= "Display registrations by training".t %> :</label>
<%= text_field_with_auto_complete :training, :title, { :size => 50,
:tabindex => 1 },
{ :select => 'mon_titre',
:after_update_element => "function(element,val)
{
var nodes = val.select(['.value_title']) || [];
if(nodes.length>0)
{
$('sortRegistration_training_id').value =
Element.collectTextNodes(nodes[0],
'value_title');
}
}"
}
%></p>
<input id="sortRegistration_training_id"
name="sortRegistration[training_id]" type="hidden" size="20" value="" />
<% end %>
If I copy / paste the code, only one textField works, I've tried to change
some parameters but still not changing something.
P.S. As I said I am new in the RoR, I do not know if you need more code
for help me so please, feel free to ask more, I will edit my question

No comments:

Post a Comment