How to get around observe_field 'template missing' error?

When my params for my observe_field are empty or 0 I get the ‘template
missing’ error. I’m trying to emulate the ‘user name is valid/not
valid’ feature that many apps have when signing up. As long as the
params is greater than zero everything is fine.

def already_taken
if params[:user].strip.length > 0
user = User.find_by_name(params[:user])
if user
render :update do |page|
page[:already_taken].replace_html “Already taken!”
page[:already_taken].set_style :color => ‘red’
end
elsif user == nil
render :update do |page|
page[:already_taken].replace_html “Available!”
page[:already_taken].set_style :color => ‘green’
end
end
end
end

There must be an easier way to do this because it is such a common
feature. Thanks for any help!

On 09 Apr 2007, at 21:17, jko170 wrote:

      page[:already_taken].replace_html "Already taken!"

There must be an easier way to do this because it is such a common
feature. Thanks for any help!

Well, you would be better off using javascript itself instead of the
rails helper and check for the value being empty clientside and only
query the server if a certain number of characters is entered, but if
you want to stick with the rails helpers, you should add:

def already_taken
if params[:user].strip.length > 0
# do your thing
else
render :nothing => true
end
end

Best regards

Peter De Berdt

Thanks a lot Peter!

I’m sure there’s a better way to do my entire project, but I guess
this is fine for now (plenty of time to learn more and refactor after
project is up).

I would recommend adding something like :condition => ‘value.length >
0’ to your observe form.

Also you can replace .strip.length > 0 with .blank?