Solved

Ruby on Rails

Posted on 2011-09-26
4
457 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 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