rails multistep form - ruby-on-rails

id like to make a multistep form with rails using the edit and update actions. so i would like it to be like step 1 of the form, and the user fills in his name, address, and phone number. then the user clicks save and continue and he then fills out his shipping address and then clicks save and continue and fills out his billing address. i saw ryan bates version, but its not what im looking for. i would like the order to be saved after the first step so if the user doesnt finish their form, i can call them and ask them what went wrong. can anyone refer me to a tutorial or give me an example of how to make an order form using the edit and update methods?

Typically this means you'll need to put conditions on your model validations. Some subset of your validations should apply to each form page:
class User
validates_presence_of :first_name
validates_presence_of :last_name
validates_presence_of :street, :if => :on_page_two?
validates_presence_of :city, :if => :on_page_two?
validates_presence_of :postal_code, :if => :on_page_two?
validates_presence_of :state, :if => :on_page_two?
validates_presence_of :country, :if => :on_page_two?
validates_acceptance_of :terms_and_conditions, :if => :on_page_three?
def on_page_two?
# whatever you need to determine the page number
end
def on_page_three?
# whatever you need to determine the page number
end
end
It's not pretty but I highly recommend a pattern like this. Anything more complicated and you'll need to rewrite it when your signup flow changes.

There are different approches to this problem.
My particular prefered solution is to implement something like a "State Machine" in the model. This way, I can persist the progress of a, per example, a multistep form, without having to hasle with more actions than new/create and edit/update.
I'm currently working on a heavy long State Machine application using the State Machine gem for rails.
Hope it helps you!

There's a great Railcast on creating multi-step wizard forms, which you can find here. It uses the Wicked gem.

Related

Relaxing validations for 'quick create'

I have a couple of models in my application that incorporate extensive ActiveRecord validations to ensure data quality, but because these require a lot of user input I'm also allowing users to quick create records by inputting only a fraction of the information typically required for a full create.
Is there a way to bypass a number of validations when I'm doing a 'quick create'?
Up until this point I've been doing #project.save(:validate=>false) and doing the necessary validations manually in the controller action but this is ungainly and creates redundant code. How should I go about this?
The following snippet is borrowed from another post and modified it a little:
class Project < ActiveRecord::Base
validates_uniqueness_of :project, :unless => :quick_create
attr_accessor :quick_create
end
The following snippet goes into your view
<%= submit_tag 'Submit', :name => 'project[quick_create]' %>
All the logic is in your model, you don't touch your controller at all.

Validations that rely on associations being built in Rails

A Course has many Lessons, and they are chosen by the user with a JS drag-n-drop widget which is working fine.
Here's the relevant part of the params when I choose two lessons:
Parameters: {
"course_lessons_attributes"=>[
{"lesson_id"=>"43", "episode"=>"1"},
{"lesson_id"=>"44", "episode"=>"2"}
]
}
I want to perform some validations on the #course and it's new set of lessons, including how many there are, the sum of the lessons' prices and other stuff. Here's a sample:
Course Model
validate :contains_lessons
def contains_lessons
errors[:course] << 'must have at least one lesson' unless lessons.any?
end
My problem is that the associations between the course and the lessons are not yet built before the course is saved, and that's when I want to call upon them for my validations (using course.lessons).
What's the correct way to be performing custom validations that rely on associations?
Thanks.
looks like you don't need a custom validation here, consider using this one:
validates :lessons, :presence => true
or
validates :lessons, :presence => {:on => :create}
You can't access the course.lessons, but the course_lessons are there, so I ended up doing something like this in the validation method to get access to the array of lessons.
def custom validation
val_lessons = Lesson.find(course_lessons.map(&:lesson_id))
# ...
# check some things about the associated lessons and add errors
# ...
end
I'm still open to there being a better way to do this.

Rails validation problem

I've got a User model with three fields, :email, :display_name and :handle. Handle is created behind the scenes from the :display_name.
I'm using the following validations:
validates :display_name, :presence => :true, :uniqueness => { :message => "Sorry, another user has already chosen that name."}, :on => :update
validates :email, :presence => :true, :uniqueness => { :message => "An account with that email already exists." }
I use the handle as the to_param in the model. If the user fails the validation by submitting a :display_name that already exists, then tries to change it and resubmit the form, Rails seems to use the new handle as the validation for the email -- in other words, it assumes that the email doesn't belong to the current user and validation on the email then fails. At this point, Rails assumes that the changed display name/handle is the one to use for the look up and the update action can't complete at all, because it can't find the user based on the new handle.
Here's the update method:
def update
#user = User.find_by_handle(params[:id])
#handle = params[:user][:display_name]
#user.handle = #handle.parameterize
...
end
This problem doesn't happen when the validation first fails on a duplicate email, so I'm assuming it's something about the way I've written the update method -- maybe I should try setting the handle in the model?
maybe I should try setting the handle in the model?
^ This.
The controller isn't the place to do something like this. If it's model logic that's happening behind the scenes, beyond the user's control, why put it in controller code?
Do it instead in a before_save filter, which is guaranteed to run only after the chosen display name is determined to be available and the record is deemed valid. In this way the handle won't be changed on the cached record until it is actually committed to the db, eliminating the problem of the incorrectly generated URL.
before_save :generate_handle
...
def generate_handle
self.handle = display_name.parameterize
end

validating presence of email address (can't be blank)- Ruby on Rails

Pretty new at all this. I have a simple form for users to enter a couple pieces of information and then input their email address and push the submit button. I want to make it mandatory that they have to fill out their email address in order to push the submit button. If they don't fill out their email address they should get an error message on the email box that says the email can't be blank. I know this is super simple but I need exact help on where to put what code. I've been researching this all night and know that part of the code should go in the application_controller and other should go in the html file where the actual text_field:email is.
I'd be grateful if someone could clearly tell me what the necessary steps are for doing this. Thanks!
It should go in your model. Add this:
class Model < ActiveRecord::Base
validates_presence_of :email
end
Check this link for more info: http://guides.rails.info/activerecord_validations_callbacks.html#validates-presence-of
In Rails 2, which I would assume you are using, validations go in the model. Which is located in $Rails_app_directory/app/model/$Classname.rb
In order to add ActiveRecord validations you can use the line
validates_presence_of :email_address
You should also consider using Rails to generate a confirmation field and filtering out ill-formatted email addresses. You could accomplish the former with:
validates_confirmation_of :email_address
with this, all you need to add to your form is a text_field for :email_address_confirmation
and the latter with a regular expression such as:
validates_format_of :email_address, :with => /\A[\w\.%\+\-]+#(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)\z/i
From snipplr, place in your model
validates_format_of :email,
:with => /^([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
:message => 'email must be valid'

Ruby on Rails ActiveRecord conditional validation (and more..)

I have a Product model which validates multiple attributes (including a Paperclip image attachment) like so:
validates_presence_of :name
validates_format_of :name, :with => /^([a-zA-Z0-9\ \-]{3,128})$/i
...
has_attached_file :image
validates_attachment_presence :image
validates_attachment_content_type :image, :content_type => ["image/jpeg", "image/png", "image/gif"]
Everything is working fine. What I want now is to make an (unobtrusive) hidden iframe in-place upload script using javascript. My problem is that I cannot just upload the image without the rest of the data, because it will fail validation (no name present) and also I cannot send the rest of the form without the image (same thing, fails validation).
So basically what I need (and don't know how to achieve) is to conditionally apply the model validations according to what the action is currently in progress (uploading the image or editing other data).
I hope I was clear enough. Any help is appreciated. Thanks.
Railscasts have a nice video screencast about conditional validations.
I hate having to wade through a video just to get a simple answer. In fact, I think that a blog entry is superior to a simple tutorial video simply for the fact that it is searchable. Here is a simple case in plain text for anyone else looking:
To validate the presence of password only for the create action, do this:
validates_presence_of :password, :on => :create
A comment for Peter D.
Thank you very much. I'm unable to watch that screen cast presently (though I plan to) and was looking for a speedy, brief answer.
Incorporated your suggestion into a model I have and it's working perfectly. (Though I suspect at some point I'm going to need validation on update when passwords are being changed. I'll take it as "technical debt" now though, in order to move on.)
The bit I added:
validates :password, :presence => true, :confirmation => true, :on => :create

Resources