Solved

Ruby on Rails

Posted on 2011-09-26
4
454 Views
Last Modified: 2012-05-12
I'm trying to create a form with Rails on windows with APache, I see that Ruby on Rails for windows is running fine.  The ruby scripts and codes seems to be working, but it doesn't displays the errors after the validation.


In another instance I'm trying to create a form with Rails using Google Spreadsheet for populating the fields with data, again I feel that scripts are running fine, but it doesn't displays the errors after the validation. Not sure whats the problem.




The rails code is as shown, I am using this script from the previous setup. I am still beginner on Ruby on Rails, any help in much appreciated.





 #users_controller.rb class
 UsersController < ApplicationController
 
    def index
        @users = User.all
    end
   
    def show
        @user = User.find(params[:id])
    end
   
    def new
        if session[:user_id].nil?
        if params[:user].nil? #User hasn't filled the form
           
    @user = User.new
        else #User has filled the form            
            user = User.new(params[:user])
                if user.save
                user.salt = rand(1000000000)
                user.password = Digest::MD5.hexdigest(user.salt.to_s + user.password)
                user.save
                flash[:notice] = 'User was successfully created.'
                session[:user_id] = user.id
                session[:password] = user.password
                redirect_to url_for(:action=>"index",:controller=>"users")
                    else render :action=>"new"
                    end
               end
        else #User is already logged in flash[:notice] = 'You are already registered.'
       
        redirect_to url_for(:action=>"index")
        end
    end
0
Comment
Question by:mahaq
4 Comments
 
LVL 4

Expert Comment

by:kristinalim
ID: 36599606
You've been getting the "new" template rendered after the form submissions you tried?

What do you want to achieve, printing the flash message in your form or printing the validation errors? Can we take a look at your form, and maybe validation in your User model too.
0
 
LVL 12

Expert Comment

by:JESii
ID: 36599694
As @kristinalim says, reviewing your view code (the form) is a good starting place, just to make sure that the error messages code is correct.

There is one another possible issue: with HTML5, validation errors are trapped at the browser and not automatically sent back to the server. I had this happen to me and it was a real puzzler. If, for example, some of your fields have a class='required' in the definition, HTML5 will simply put the cursor back on the required (empty) field and say nothing.  To check this, do a "View Source' from your browser. You can also see if there is actually a "round trip" being made to your server and back, or if it's just a cursor re-positioning action taken in the browser.
0
 
LVL 2

Accepted Solution

by:
WizardOfOgz earned 500 total points
ID: 36710120
Your exact problem isn't clear to me.  Are you trying to show the error messages from the flash, or are you trying to highlight the fields with validation errors?

If you want to display the flash messages you must explicitly do so in your view template or layout.  I add a method to the application_helper file.
module ApplicationHelper
  def flash_messages
    messages = ''
    flash.each do |type, msg|
      messages << content_tag(:div, msg, :class => "flash-message flash-#{type}")
    end
    messages.html_safe
  end
  ...
end

Open in new window


And then call it from my layouts and templates
<%= flash_messages %>

Open in new window


If you want to show fields with validation errors it would help if you posted your view code.  Here is a list of things to check for:
1. Ensure that your validations are adding errors to the correct fields.  You can use a debugger to check this or print out debug info on your page.  Something like
<%= debug @user.errors %>

Open in new window

should show you what errors are on your user model instance when the template is rendered.
2. Your form is generated with proper DIVs/SPANs/classes.  Using Rails form helpers you will have a DIV with class ".field_with_error" around each invalid field and label.
3. Error fields are styled with CSS.
0
 

Author Closing Comment

by:mahaq
ID: 36945296
I could not get this done and left it out. Anyway thanks for your suggestion and help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to SSH interactive Session In ruby Ruby 3 1,118
find word then return thsat line plus next line 4 344
Install apt-get and ufw on Mac 10.8.4 6 1,001
What does  =~ mean in Ruby 1 482
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now