Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You seem to be mixing up stuff.</p> <p>You have html inside your controller-method? That should be in your view. For each controller-method, normally a view with the same name is rendered, except if you explicitly call <code>render</code> from within your controller-method.</p> <p>You do not write html in your controller. You write html in the view, and sometimes you have helpers to make your views more readable.</p> <p>Secondly, in your first piece of code, which is some view-code, i hope. The view is prepared at server-side and then sent to the client. You can render another view, a partial, from a view. But this does not load data live.</p> <p>How i would fix this. Inside your views where you want to dynamically render the validation:</p> <pre><code>&lt;script&gt; $("#validate_company").live("keyup", function() { $("#company_info").load("&lt;%= url_for :controller =&gt; 'live_validations', :action =&gt; 'validate_client_company' %&gt;"); }); &lt;/script&gt; </code></pre> <p>Then inside your controller you write:</p> <pre><code>class LiveValidationsController &lt; ApplicationController def validate_client_company if params[:first_name].length &gt; 0 @client = Client.find_by_company(params[:company]) @error = @client.nil? ? :valid_username : :invalid_username else @error = nil end render :partial =&gt; "message", :layout =&gt; false end end </code></pre> <p>Inside your <code>app/helper/live_validations_helper.rb</code> you add a method</p> <pre><code>def get_validation_message(error) if error == :invalid_username image_tag('/images/cancel_48.png', :alt =&gt; 'Invalid Username') + "Name taken" elsif error == :valid_username image_tag('/images/accepted_48.png', :alt =&gt; 'Valid Username') end end </code></pre> <p>and inside your <code>message</code> view you write something like:</p> <pre><code>&lt;%= get_validation_message(@error) %&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload