Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
displaying error messages in ror
6 Posts
displaying error messages in ror

Hi,iam doing project in ror,Iam unable to get error messages of email format,blank etc...i want popup window to display these messages.plz any one can help me.

If by getting error message of email format you are referring to ActiveRecord validation, then these are available through the ActiveRecord#errors methods. For instance:

bq. class User < ActiveRecord::Base

validates_presence_of :name, :email
validates_format_of :email, :with => /^.+@.+\..+$/

end

In your controller, let's say you get your user attributes through the params[:user] hash.

bq. class UsersController < ApplicationController

def create
    @user = User.new(params[:user])
    unless @user.valid?
        flash[:errors] = @user.errors
    else
        redirect_to user_url(@user)
    end
end

end

Finally, in your view:

bq.

Hi Ulisses Reina Montenegro.Thank u for giving me reply.But the view code giving me syntax errors like

syntax error, unexpected kNOT, expecting ')' erbout.concat "\t\t "; unless flash[:errors].nil? ; erbout.concat " "; _erbout.concat(( update_page_tag do |page| page.alert(“Could not save user because:\n\n” + flash[:errors].full_messages.join(”\n”)) end ->

syntax error, unexpected $undefined, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END erbout.concat "\t\t "; unless flash[:errors].nil? ; erbout.concat " "; _erbout.concat(( update_page_tag do |page| page.alert(“Could not save user because:\n\n” + flash[:errors].full_messages.join(”\n”)) end ->

syntax error, unexpected $undefined, expecting ')' erbout.concat "\t\t "; unless flash[:errors].nil? ; erbout.concat " "; _erbout.concat(( update_page_tag do |page| page.alert(“Could not save user because:\n\n” + flash[:errors].full_messages.join(”\n”)) end ->

syntax error, unexpected $end, expecting ')'

I changed the end's and tried diferently but not working.I put this view in layout.And also worked in rhtml Iam unable to get it.plz give me advice

Don't blindly paste code without understanding what it is doing. :-)

In this case I think the error is simply that there is a missing '%' at the end of the tag containing the call to update_page_tag. i.e.:

Thanks a lot for the fix, Jon. :-)

Keep in mind that traditional javascript "popups" are very out of date. Instead use a div layer and display the flash message inside.

flash[:key] = "This will put a flash into the session and after render or redirect it will be available to the request." flash[:key].now = "This will put a flash message into the current response only, so if you redirect it will be gone, but a render will show it"

You could use JQuery to check to see if the div has content (ie a flash message) and if it does, show the layer and center it.

Good luck.

Person
Sr. Ruby & Rails Consultant For Hire
Sign In To Rate Post
6 Posts
Login to add your message