Solved

Ruby on Rails

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
distance_of_time_in_words_to_now method rails app 9 707
Rails installation ? 6 828
Ruby problem with blocks 5 312
Ruby Cucumber - training course 1 698
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…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

22 Experts available now in Live!

Get 1:1 Help Now