Wednesday, December 15, 2010

Rails

SELECT
Customer.find(:all, :conditions=>["name LIKE ?", @message.to])

INSERT
@customer = Customer.new(:name => @message.to)
@customer.save

Autocomplete
1. app/views/layouts/messages.html.erb (javascript in header)
<%= javascript_include_tag :defaults %>
2. app/views/messages/new.html.erb (html form)
Replace
<%= f.text_field :to %>
with
<%= text_field_with_auto_complete 'message', 'to',{}, :skip_style => false  %>
3.app/controllers/messages_controller.rb (SQL select)
  def auto_complete_for_message_to() 
    user_name = '%' + params[:message][:to] + '%' 
    @customers = Customer.find(:all , :conditions=> ["name like ?", user_name.downcase]) 
    render :partial => 'username' 
  end
4. app/views/messages/_username.html.erb (partial output that is generated)

    <% for customer in @customers do %>
  • <%=h customer.name %>
  •  <% end %>

No comments: