Posted by Jim Morris
Wed, 25 Oct 2006 21:08:38 GMT
I just ran into this little problem which kept me scratching my head
for quite a while. My flash message didn't show up on the next action.
I have a status page which polls a database for status using
periodically_call_remote, when the database reaches a certain state I
show a button which says the process is complete go to next step.
The go to next step calls a controller action which does some processing
that can take a few seconds, during that call any errors are
dutifully written to flash like flash[:error]= "I got an error!"
then does a redirect_to the listing page, where I expect to see the
error flash at the top of the page.
This worked fine under development however under production on the
production server I did not see those flash errors, so the user was
like "duh what happened??".
I could see the errors logged in the production log so I knew the
flash was getting set, so what was happening????
Well to cut a long story short, while the controller was processing
the next action (which can take a few seconds), the
periodically_call_remote can fire a few more times, this consumes
the flash, so when it gets to the redirect_to page the flash no longer
shows up. In development mode the timing is different so the status
page does not fire again before the listing page shows up so the flash
is displayed. Did I mention I hate race conditions :)
The solution is trivial, stick a flash.keep in the action that is
called by the periodically_call_remote, and the flash is preserved
for the redirect_to action. Problem solved!
Posted in Rails | Tags flash, periodically_call_remote, rails | no comments | no trackbacks
Posted by Jim Morris
Mon, 23 Oct 2006 20:58:46 GMT
I ran into this problem a few times,and I have seen others asking the
same question, if you use text_field_with_auto_complete and the
selection list returns non-unique results, how do you reference the
actual record in the database you want?
For instance if you have text_field_with_auto_complete :customer, :name
then in your controller: name= params[:customer][:name] and
Customer.find_all_by_name(name) returns more than one entry you need
to be a little more tricky to retrieve the actual record you wanted to
select.
There are a couple of ways to do it.
One method is mentioned here:
http://www.dalemartenson.com/blog/?p=24
which hides the id field being returned.
Another method is here
http://ricardo.pacheco.name/blog/articles/2006/09
which uses javascript to write the id into a hidden_field.
This wiki entry
(about half way down) suggests another way to do it, putting
the id in the id tag of the <li> andf fetching it with javascript.
Another method I use is to put in the text_field a string like
"23,Blogs,Fred", this is the id of the customer record, and the
last,first name. Then I do this in the controller method that receives
the form data (eg def create) ...
namecsv= params[:customer][:name]
id,last,first= namecsv.split(',')
customer= Customer.find(id)
Although in reality I usually write a setter in the Model to handle
the csv so I can simply pass the entire params to the model ie
Customer.new(params)
I get the namecsv in the text box using this partial for the auto
completer...
<ul class="auto_complete">
<% for customer in @customers do -%>
<li class="big">
<div class="name"><%=h customer.fullname -%></div>
<div class="code"><%=h "#{customer.id},#{customer.lname},#{customer.fname}" -%></div>
<div class="email">
<span class="informal"><%=h "#{customer.email}" -%></span>
</div>
</li>
<% end -%>
</ul>
using this in the view...
<% text_field_with_auto_complete( :customer, :name, {}, {:select => 'code', :skip_style => true) %>
Notice the :select => 'code', this is critical as it tells it which part of the popup list to put
into the text_field.
This is a little ugly and error prone so you need some error checking etc.
The other method looks nicer on the screen but is more work in the background.
Posted in Rails | Tags rails, text_field_with_auto_complete | 1 comment | no trackbacks
Posted by Jim Morris
Wed, 18 Oct 2006 06:45:47 GMT
I ran into a problem using text_field_with_auto_complete in a view
where I wanted to have many of them created by an iteration. You can
use the :index option for the text_field, but it doesn't carry over to
the various divs used in the AJAX calls.
<% 0.upto(10) do |legi| %>
<% @leg= @mission.legs.find(:first, :conditions => ['legnum = ?', legi]) %>
text_field_with_auto_complete( :leg, :name, {:index => legi, :size => 20})
<% end %>
<% end %>
is what you really want to do, just as for a regular text_field.
I googled around and found this bug
report for a fix to
text_field_with_auto_complete that allows :index, as that fix does not
appear to be in the current stable release of Rails or on Edge rails, I just created a
my_text_field_with_auto_complete and it worked like a charm!!
def my_text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
if(tag_options[:index])
tag_name = "#{object}_#{tag_options[:index]}_#{method}"
else
tag_name = "#{object}_#{method}"
end
(completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
text_field(object, method, tag_options) +
content_tag("div", "", :id => tag_name + "_auto_complete", :class => "auto_complete") +
auto_complete_field(tag_name, { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options))
end
So I put this in app/helpers/application_helper.rb and simply use
my_text_field_with_auto_complete in my views.
Here is what the controller side looks like...
def auto_complete_for_leg_name
leg= params[:leg].keys[0]
auto_complete_responder_for_name params[:leg][leg][:name]
end
...
private
def auto_complete_responder_for_name(value)
param= value.downcase + '%'
find_options= {
:conditions => [ 'LOWER(lname) LIKE ?', param ],
:order => 'lname ASC',
:limit => 6
}
@names = Person.find(:all, find_options)
render :partial => 'names'
end
Posted in Rails | Tags rails, text_field_with_auto_complete | 18 comments | no trackbacks
Posted by Jim Morris
Sun, 01 Oct 2006 00:20:00 GMT
I use Lugarus excellent Epsilon Editor for most of my
programming editing needs, on Win32 and Linux.
(An exception is for Java programming where I use Eclipse).
I have spent some time writing extensions to Epsilon to handle Ruby
and Rails programming, inspired mostly by Textmate on Mac/OSX, and
Eclipse for Java. I tried using the Eclipse for Ruby, but I was very
disappointed, also the developers have made some design decisions I
can't live with (like no auto indent after opening braces etc).
I have tried Komodo Pro as described
here.
OK the Epsilon editor is not free, it costs about $250 ($99 for an
upgrade) which includes all platforms, and the license lets you use it
on your Laptop and Desktop, in fact you can use any version of Epsilon
on up to four computers you own. The result is a professional, solid,
stable Editor, that is what you get when you pay for something. (This
editor has been around at least 20 years, which is when I started
using it on DOS!). In addition to running as an X Windows program and
a console program on windows and Linux it also runs on Mac OS X,
FreeBSD, OS/2 and DOS.
The Editor is an Emacs clone out of the box, it also has CUA and Brief
emulations (well key bindings). The best feature IMHO is the fact you
get most of the source code for the editor which is written in its own
c-like language called eel. This makes it much easier to write
extensions and customizations for the editor if you are familiar with
C. (I never could wrap my brain around Lisp which is why I don't use
GNU Emacs). It also runs in console mode as well as windows mode,
which is useful if you have to login via ssh etc to edit files.
Seeing how every programmer has their own ideas of what an editor
should do and its look and feel, easy customization is crucial.
The extensions I have written for Epsilon are all freely available, as
are extensions written by other users. (Various language modes,
template extensions, SCM extensions etc).
In the past I wrote a java help extension that tried to do context
sensitive help, this worked OK but not as well as Eclipse.
Recently I wrote a bunch of extensions for Ruby and Rails, thanks to
the ruby mode extension written by Timothy Byrd (available on Lugaru's
download page) I was able to get syntax highlighting and formatting
already done. I added some simple help extensions, a snippet facility
(ala Textmate), and some convenience actions for Rails development. I
also extended Timothy's ruby mode extension with something that
completes the #{} when # is typed in a string. (I first saw this in
Textmate and hated it, but it grew on me, until I had to have it on
Linux). I have avoided the temptation to also do the automatic closing
of { ( " etc that you find in Textmate because I still hate those, but
they are easy to do using the same technique I used for #{}.
I've also added the ability to run the current buffer through the ruby
interpreter and show the results in a pop up window, also to run a
specific unit test if the file is a Ruby test case.
The key strokes any command uses is easily modified, as well as the
colors used for syntax highlighting.
The version of ruby_mode.e on Lugarus site does not currently have the
latest changes I have made, so it can be downloaded from the link below...
The other extensions can also be downloaded from my site...
These can be installed by copying them to your ~/.epsilon folder, and
adding a load line to your einit.ecm file, see the comments in the
source file. The snippets should be un-tarred into the ~/.epsilon
folder. The README explains how to load them.
eg add this to your einit.ecm file:
(load-eel-from-path "ruby_mode.e" 2)
(load-eel-from-path "rubyhelp.e" 2)
(load-eel-from-path "snippets.e" 2)
(load-eel-from-path "rename_in_place.e" 2)
Also here are the color codes I use for ruby_mode, these are designed
by Timothy: (Change the window-black to whatever color set you are using)
&window-black color class for ruby-brace: [0x725CEB on 0x0]
&window-black color class for ruby-class: [0xD9D240 on 0x0]
&window-black color class for ruby-comment: [0xC0C0C0 on 0x0]
&window-black color class for ruby-global: [0xFFB737 on 0x0]
&window-black color class for ruby-keyword: [0xFF8000 on 0x0]
&window-black color class for ruby-number: [0xFF9090 on 0x0]
&window-black color class for ruby-punctuation: yellow on black
&window-black color class for ruby-regexp: [0x007FFF on 0x0]
&window-black color class for ruby-shell-cmd: [0x8FFF2F on 0x0]
&window-black color class for ruby-shell-subst: [0x8FFF8F on 0x0]
&window-black color class for ruby-str-subst: [0xFFC0C8 on 0x0]
&window-black color class for ruby-string: cyan on black
&window-black color class for ruby-perl-var: red on black
Although you can browse for files that epsilon has currently open and
switch between these buffers, the method is fairly crude by todays
standards of tabbed windows etc, so I wrote a little graphical helper
called
project_browser.rb
that uses the fox window toolkit. It just shows a tree of the
directory it was given on the command line, and if you click on any of
the files they open in the epsilon window. This is a lot like the
project browser windows you find in Textmate, Eclipse and others. You
need to install fox version 1.4 and the fox14 gem too to use this. It
also allows you to exclude files and directories from display in the
tree, by putting a YAML file called .proj_exclude.yaml in the
project directory, I'll document this further if there is any interest
in it (Leave a comment if you are interested). It allows multiple
project directories to be open and shows them in tabs at the top. I'm
also working on integrating subversion into it. It could also be
adapted to work with virtually any editor that allows files to be sent
to the editor by a separate process.

Posted in Rails, Ruby, Linux | Tags editor, epsilon, ide, rails, ruby | no comments | no trackbacks