<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sharjeel&#039;s Personal Blog &#187; tricks</title>
	<atom:link href="http://blog.sharjeel.info/tag/tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sharjeel.info</link>
	<description>life stories, travel, photography, music, short movies, programming, entrepreneurship and startups</description>
	<lastBuildDate>Sat, 27 Nov 2010 01:37:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Ruby on Rails validation messages in RJA</title>
		<link>http://blog.sharjeel.info/2009/07/06/ruby-on-rails-validation-messages-in-rjs/</link>
		<comments>http://blog.sharjeel.info/2009/07/06/ruby-on-rails-validation-messages-in-rjs/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 03:05:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rjs]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://blog.sharjeel.info/?p=11</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://blog.sharjeel.info/2009/07/06/ruby-on-rails-validation-messages-in-rjs/' addthis:title='Ruby on Rails validation messages in RJA'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>This thing wasted my few<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.sharjeel.info/2009/07/06/ruby-on-rails-validation-messages-in-rjs/' addthis:title='Ruby on Rails validation messages in RJA' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>This thing wasted my few hours and then finally I made it work. It&#8217;s very easy to show validation messages in Ruby on Rails on the page when It reloads after submitting the form. Just use one following line to show all the validation error messages.</p>
<blockquote><p>&lt;% form_for :model, :url =&gt; &#8216;url&#8217; do |f| %&gt;<br />
&lt;%= f.error_messages %&gt;  #print all the validation error messages<br />
#here goes all of your other code for other fields<br />
&lt;% end %&gt;</p></blockquote>
<p>Now the question is how to show same validation in RJS while working with AJAX? error_messages does not work in RJS. You can access @model.errors array in RJS but it&#8217;s an array having all the error messages. You can use following code to print error messages in your RJS page to update original page view.</p>
<blockquote><p>page.replace_html &#8220;error_messages&#8221;,@model.errors.full_messages.join(&#8220;&lt;br&gt;&#8221;)</p></blockquote>
<p>Replace @model with your model name you are working with. Function full_messages will print all of your error messages but if you use join function and pass &lt;br&gt; as parameter then it will join all the error messages and add a line break in between.</p>
<p>Here is another way of using this error message. You can also store validation messages in a flash message and then use either in RJS or your view. Write following code in your controller action.</p>
<blockquote>
<pre>if @model.save
   #when successfully saved
else
   flash[:error] = @model.errors.each {|field, msg| error &lt;&lt; msg + "&lt;br&gt;"}
end</pre>
</blockquote>
<p>that&#8217;s it!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sharjeel.info/2009/07/06/ruby-on-rails-validation-messages-in-rjs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

