Skip to content

Archive for July, 2009

8
Jul

Life Status

9th July 2009 – an ordinary summer night

2AM: Summer Nights are mostly not dark in Sweden but today it’s.one summer night

It’s raining like crazy and I’m loving it. It reminds me of home but I guess Swedes do not really like rain (they like sun more!)

Sitting in my apartment all alone here in Stockholm.

I haven’t eaten my dinner yet thinking to fry chicken or something.

SVN server is down at the moment so I can’t really work.

about work well, I’m not really doing any serious work mostly wasting time and 1 or 2 hours of programming or related things

I have 3 web based projects in my mind on which I want to start work as soon as possible but this INTERNET wouldn’t let me work. so many other interesting things to do here.

I haven’t watched a single movie in last three months

Just noticed mostly I listen sad songs. I guess I need to shut down Spotify and start listening some Radio that plays happy songs

two and a half men

Listening “Kaminey” from the new Bollywood movie “Kaminey” on repeat. It’s not like I’m a BW fan. It’s not about BW it’s about Vishal B.

I would like to end this blog post with trailer of “Trainspotting” the movie, do watch!

Trainspotting – U.K. Trailer from you_want_elvis on Vimeo.

6
Jul

Ruby on Rails validation messages in RJA

This thing wasted my few hours and then finally I made it work. It’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.

<% form_for :model, :url => ‘url’ do |f| %>
<%= f.error_messages %>  #print all the validation error messages
#here goes all of your other code for other fields
<% end %>

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’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.

page.replace_html “error_messages”,@model.errors.full_messages.join(“<br>”)

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 <br> as parameter then it will join all the error messages and add a line break in between.

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.

if @model.save
   #when successfully saved
else
   flash[:error] = @model.errors.each {|field, msg| error << msg + "<br>"}
end

that’s it!

Лабрадор