SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validating forms with RoR

    I am trying to validate a simple form with RoR, however its not validating at all.

    I am going with the flow of the "Agile web development with Rails" book. I am at the point of starting validation with my form and it allows the empty form to be submited anyways.

    I edited app/models/product.rb with the following..

    Code:
    class Product < ActiveRecord::Base
        validates_presence_of :title, :description, :image_url
    end
    I am using Rails 1.2 (in vendor/rails)... I thought everything goes through the model layer (as I've read through this book), before going to the database..

    Any help on this issue would be greatly appreciated!

  2. #2
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeraych View Post
    I am trying to validate a simple form with RoR, however its not validating at all.

    I am going with the flow of the "Agile web development with Rails" book. I am at the point of starting validation with my form and it allows the empty form to be submited anyways.

    I edited app/models/product.rb with the following..

    Code:
    class Product < ActiveRecord::Base
        validates_presence_of :title, :description, :image_url
    end
    I am using Rails 1.2 (in vendor/rails)... I thought everything goes through the model layer (as I've read through this book), before going to the database..

    Any help on this issue would be greatly appreciated!
    the first thing i'd try is to restart webrick. this is the first thing i do when i have some unexpected behavior and it solves the problems sometimes.

    your code looks right.

    i take it that when you say the form is submitted that it is actually stored in the database, too.

  3. #3
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not running webrick, I am just using apache to run everything.. :O

    and yes, posting to a database. It skips validation and goes right to the database.

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeraych View Post
    I am not running webrick, I am just using apache to run everything.. :O

    and yes, posting to a database. It skips validation and goes right to the database.
    did you restart apache and try it? i assume so, but you didn't say.

    i'm also having an issue with the validation - posted below. i'm running the latest cvs version.

    your code looks right - and it is pretty trivial code. if restarting apache isn't the problem, i'm at a total loss what is the problem.

    good luck.

  5. #5
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok lol anyone else have any ideas of why this is happening? The controller code is this..

    Code:
    class AdminController < ApplicationController
       scaffold :product
    end
    And no matter what I try, it wont validate.. still.

    I know I could continue on and learn more about other aspects of rails, but I would like to get validation down at least lol.

  6. #6
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeraych View Post
    Ok lol anyone else have any ideas of why this is happening? The controller code is this..

    Code:
    class AdminController < ApplicationController
       scaffold :product
    end
    And no matter what I try, it wont validate.. still.

    I know I could continue on and learn more about other aspects of rails, but I would like to get validation down at least lol.
    it should work. i'm at a loss. hopefully someone else will step up here. it is just bizarre.

    i might even delete the directory and do it all over again and see if i get the same error. the extra practice is probably a good thing.

  7. #7
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know! I have searched all over google with something along the lines of "validation with rails" etc.. and everywhere I go, its the same exact syntax... I have no clue what I am doing wrong, I have even copied it directly from the book's pdf to see if it would work, and I am definately not missing anything, I have redone the code countless times, and I am completely lost

  8. #8
    SitePoint Enthusiast thebasti's Avatar
    Join Date
    Aug 2004
    Location
    Novi Sad, Serbia
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jeraych,

    I did go through that same example from Agile book and it worked, so it must be something on your end...

    Since you are sure that you have reloaded the whole app, perhaps the problem is in view file?
    "Strategy without tactics is the slowest route to victory.
    Tactics without strategy is the noise before defeat."
    - Sun Tzu
    Icebergist - a web dev blog | Orange Iceberg Web App Development

  9. #9
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there is no view file... I am just using the basic scaffolding..

    scaffold roduct

    ya know?

  10. #10
    SitePoint Enthusiast thebasti's Avatar
    Join Date
    Aug 2004
    Location
    Novi Sad, Serbia
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I didn't read your messages carefully. I just presumed that you used generate script which makes the view files.

    Anyway, I just tried to make a Rails app using code you posted and it works just fine. I didn't do anything special: created app, created model and controller file (copy&paste) - and it works.

    My guess it that this is server issue. As skeeterbug said this looks like a typical "reload" problem. Are you completely sure that you reloaded Apache and app itself (depending on your setup this might be a separate step)?

    You should also try to start the app using Webrick and see if it works. Additionally, you can try to open up a console and manually try to create a model and see if it goes through despite the validation.
    "Strategy without tactics is the slowest route to victory.
    Tactics without strategy is the noise before defeat."
    - Sun Tzu
    Icebergist - a web dev blog | Orange Iceberg Web App Development

  11. #11
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What steps did you take specifically to do everything? I will try doing exactly what you did to see if it works.

  12. #12
    SitePoint Enthusiast thebasti's Avatar
    Join Date
    Aug 2004
    Location
    Novi Sad, Serbia
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't done anything unusual.

    - created rails app with 'rails test'
    - created mysql database & table (via phpMyAdmin)
    - edited database.yml
    - using text editor created app/model/product.rb using your code
    - same for app/controller/admin_controller.rb
    - started webrick using './script/server'
    - loaded http://localhost:3000/admin in browser

    That's it. Also you can try to do the same thing from console.

    Code:
    basti@bali:test$ ./script/console 
    Loading development environment.
    >> p = Product.new
    => #<Product:0xb741afe8 @new_record=true, @attributes={"image_url"=>"", "title"=>"", "description"=>""}>
    >> p.save
    => false
    >> p.title = 'test'
    => "test"
    >> p.image_url = 'test'
    => "test"
    >> p.description = 'test'
    => "test"
    >> p.save
    => true
    As you can see when I first tried to save the model it returned false because validation failed.
    "Strategy without tactics is the slowest route to victory.
    Tactics without strategy is the noise before defeat."
    - Sun Tzu
    Icebergist - a web dev blog | Orange Iceberg Web App Development

  13. #13
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeraych View Post
    What steps did you take specifically to do everything? I will try doing exactly what you did to see if it works.
    Jeraych, are you *absolutely* sure you've restarted apache?

  14. #14
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you using fastcgi or normal cgi? If you're using normal cgi you don't need to restart. Are you using the development environment?

    It's easier to use webrick or mongrel for development. They're fast enough + you don't need to restart them.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •