AJAX Rails Validation - ruby-on-rails

I have my form and validation working great with regular http requests. I would like it to use AJAX. I know i can validate on the client side but that seems redundant given that I have defined the validations in my model.
As the user is filling out the form, I'd like to give feedback to them on their entries. What is the best way to use the rails defined validations in an AJAX form and give live feedback?

Check out the live-validations plugin. There's also an introductory screencast.

For Rails 3 check out Client Side Validations: https://github.com/bcardarella/client_side_validations
Here's the railscast: http://railscasts.com/episodes/263-client-side-validations

Live-validations was kinda messy to get working for me, so I started with my own solution from scratch backed by Validatious. It's actually really DRY because of the Rails conventions in the back that made it possible to do a lot of smart assumptions. In most cases, all you need is to include a few javascript dependencies and declare your validations in your models as always - and it all just works! =) Peep the README for details.
Here it is:
http://github.com/grimen/validatious-on-rails

If you're looking for a solution to this that does not introduce any plugin dependencies check out my screencast on the issue:
AJAX Validations on Rails 2.3.8

https://github.com/augustl/live-validations/wiki has installation instructions.
When you add LiveValidations.use :jquery_validations to the bottom of your environment.rb, make sure it is outside of the Rails::Initializer block.

Related

Live form validation using with jQuery/Coffee AJAX Rails

Okey, so I have this problem... And I've had it for like weeks. I'm working on a school project and I'm supposed to make a live form validation using Ruby on rails and ajax (jQuery/Coffee). I've google my arse off and I've found loads and loads of plugins for it, but not one helpful tutorial. I've got some code together that seems legit, but I need help to make this code validate the form live, checking the database for email, username etc. It's for the sign up page so I think you get the idea.
$("#new_user").live "ajax:beforeSend", (event, xhr, status) ->
form = $(this)
form.validate {
# Validations goes here, but how do I write it?
}
false unless form.valid()
So I guess I'm suppose to write a controller to handle the validations through this jQuery. I'm really new to both Rails and Ajax, so please help me out here.
Your school project will probably be due by now, but here goes anyway.
Check out https://github.com/bcardarella/client_side_validations.
If you use this, you don't need to worry about writing javascript to validate your form. You just need to worry about writing the correct validations in your model.
This might help get you started http://railscasts.com/episodes/263-client-side-validations.
Also if you are new to rails you should know that for most common problems and for a lot of not so common problems there is a solution in the form of a gem.
So if you need to write sign-in/sign-up functionality you might want to check out https://github.com/plataformatec/devise. Which also has a railscast http://railscasts.com/episodes/209-introducing-devise.
Finally here is wiki page that explains how to make client_side_validationsand simple_form work together https://github.com/bcardarella/client_side_validations/wiki/Using-Devise.
Hope this helps.

Can't use "acts_as_url" in ruby on rails 3

I'm newb and I'm sorry because of my dumb question! Please, help me!!
I'm working with Rails 3, and this my problem:
I have a model name: Photo using gem 'mongoid'.
I want to make a permalinks which are readable url instead of unreadable '_id' generated from mongoid!
After searching in google, I found a gem called 'stringex'! I decided to use this gem, and put this line in my Gemfile:
gem 'stringex'
then ran "bundle install' to use it.
I just do everything following the guide in github Readme_rsl/stringex but the trouble occurs:
undefined method `acts_as_url' for Photo:Class
Is it because ROR 3 doesn't support this gem? Or I missed something?
Please, I need help!
This is probably not the answer you're looking for, but I think the answer is "you can't do that." At least not currently.
The gem you mention is intimately tied to ActiveRecord (see this issue ticket). If you look in the stringex source code in lib/stringex.rb, you can see that acts_as_url is only included on ActiveRecord and not on Mongoid.
What's happening is that you're using Mongoid on your model, and the nice acts_as_url methods are not attached to the Mongoid::Document. It MAY be as simple as just modifying lib/stringex.rb to also include acts_as_url on Mongoid, though I assume that if it were that simple it would already have been done.
So where does that leave you? There are other ways to generate slugs. I haven't used any of them, so I can't speak to which ones are good or not. Googling "mongoid slug generation" can hopefully point you in the right direction.
Your model needs to have a url attribute to store the nice url in the database for later use.
I don't know why the don't mention that on the README. I found that out after reading a couple of articles.
In my case restarting the rails server command helped.
This isn't difficult to write yourself. And it will help you to understand a few parts of rails and routes.
Add a 'permalink' column to the db.
Do a find_by_permalink(params[:id]) in the controller
Add a def to_param method to the model and return the permalink
column
Add a create_permalink before_validations method. Generate the
permalink however you like and store it in the db.
The only trick is ensuring uniqueness and handling name (and url) changes. stringex looks like it helps with uniqueness, not with name changes.
There are other gems to help with name changes (aliasing and redirecting if there's a new name) if you care about that. You can handle that in different ways.

Subscribe to newsletter form rails 3

I'm new to rails and I would like to know how to make a form that submits to a database. I have tried devise but it seems that it deals with logins/users and it's not working for my purpose. I don't want anyone to do it for me, I have to learn :).
Creating a form that saves to a database is probably on of the most basic tasks to do with rails and is even handled in the "Getting Started"-Chapter in the Rails-Guides:
http://guides.rubyonrails.org/getting_started.html
Just work on that and you'll get the idea. It looks like it's a lot to do on that page, but it explains also the principles and all that. It becomes really interesting for you in chapter 5 and 6.
Creating a form With Rails 3 is exactly like in Rails 2.

Rails authorization plugins

We are evaluating plugins for Authorization in Rails. The two at the top of our list are
cancan and declarative_authorization.
I would like to get some feedback from anyone currently using either of these plugins.
The problem we are going to face with any authorization plugin is that we have a
database per customer model and will need to modify the plugin to work within that
model. Because of this fact I'm interested in hearing from anyone who has had to tweak the
plugins at all as well.
I'm just starting to look around at the code. It seems like cancan might be a little easier to customize.
Any thoughts?
Cancan is a lighter weight plugin for smaller sites. You can see a video on railscasts.
http://railscasts.com/episodes/192-authorization-with-cancan
I've used declarative_authorization with authlogic/restful_auth for several projects. It has everything you would need. 1) Model security. 2) Controller security 3) methods available to the view to check auth.
The only frustrating thing I've run into with declarative_authorization is me not reading the rdocs.
http://railscasts.com/episodes/188-declarative-authorization
Authority
I'd suggest you also check out my new gem, Authority. Because you do the actual logic in plain Ruby classes and methods, you can check any data source you need to: different databases, static files, phases of the moon via a web request, you name it. :)
I ended up using declarative_authorization. Now it seems that auth_logic is where the community is headed.
declarative_auth would have been really simple if it wasn't for our apps multi-tennant db
model. I had to modify the source a bit to make it all work, but it wasn't too tough to do, and
I was pretty green when I started this project.
It seems like you really can't go wrong with any of the solutions. cancan seemed cool too
but it would have needed more mods for what I was doing so I decided against it.
Its written by Ryan Bates though which is cool. Love rails casts! :)
I know this post is old but I figured I'll update any because you never know.

Ruby on Rails, AJAX examles

I'd like to know what people think what websites are good examples of AJAX with Ruby on Rails at the moment.
I'm learning both and would like to see some good interesting examples of what can be done.
THanks,
Joe
http://haystack.com/
http://rubyonrails.org/applications
These links will open a new horizon for you.
Perhaps grab a copy of http://www.redmine.org/ and look it over. Maybe not the bleeding edge of ajax but there is some good stuff in there all around for rails development. I learned a lot from it.
If you're just getting started and interested in unobtrusive AJAX with jQuery, I have a blog post you might be interested in. Basically it takes a simple scaffold generated site and adds unobtrusive deleting with jQuery step by step - if that sounds like something you'd be interested in feel free to check it out.
Twitter.com is an excellent example of what can be done with AJAX. I believe the site is coded in Ruby on Rails as well.

Resources