ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2010年2月21日 星期日
multi-step
RoR Multi-step forms
private<br /> def get_partial_user_from_session<br /> unless @session['partial_user'].nil?<br /> @user = @session['partial_user']<br /> else<br /> @user = User.new<br /> end<br /> end<br /><br /> def save_partial_user_in_session<br /> unless @user.nil?<br /> @session['partial_user'] = @user<br /> end<br /> end<br /> <br /> # Might be a good addition to AR::Base<br /> def valid_for_attributes( model, attributes )<br /> unless model.valid?<br /> errors = model.errors<br /> our_errors = Array.new<br /> errors.each { |attr,error|<br /> if attributes.include? attr<br /> our_errors << [attr,error]<br /> end<br /> }<br /> errors.clear<br /> our_errors.each { |attr,error| errors.add(attr,error) }<br /> return false unless errors.empty?<br /> end<br /> return true<br /> end<br /><br /><br />#To ensure the errors are cleared out properly on each request add a before_filter with the following:<br /> def clear_stage_errors<br /> unless @session['partial_user'].nil?<br /> @session['partial_user'].errors.clear<br /> end<br /> end<br /><br />=begin<br />You can use<br />get_partial_user_from_session<br />and<br />save_partial_user_in_session<br />as before and after filters, respectively, so you only have to reference @user in your multi-step controller.<br />=end<br /><br />#A clean way to arrange the view is to have each step in its own partial, and set a variable such as<br />@current_stage<br />#in your controller, then in your view just do something like:<br /><%= start_form_tag({:action=> "signup"} , { :name => 'signupform' }) %><br /><%= hidden_field_tag "changeCountry" %><br /><%= hidden_field_tag "current_stage", "#{@current_stage}"%><br /><br /><h2>Registration Process</h2><br /><%= @flash['notice'] %><br /><br /><%= render_partial "#{@current_stage}" %><br /> <br /><%= end_form_tag %><br /><br /><br />#Your signup controller action could something similar to this:<br /> def signup<br /> case @request.method<br /> when :post<br /> @current_stage = @params['current_stage']<br /> if @current_stage == "stage1"<br /> @user.attributes = @params['user'] <br /> @current_stage = "stage2" if valid_for_attributes(@user,["login","attribute2"])<br /> end<br /> elsif @current_stage == "stage2"<br /> @user.attributes = @params['user'] <br /> @current_stage = "stage3" if valid_for_attributes(@user,["attribute3","attribute4"])<br /> elsif @current_stage == "stage3"<br /> @user.attributes = @params['user']<br /> <br /> if @user.save<br /> @session[:user] = User.authenticate(@user.login, @params['user']['password'])<br /> flash['notice'] = "Signup successful"<br /> redirect_to :action => "home"<br /> end<br /> end<br /> when :get<br /> @current_stage = "stage1"<br /> end<br /> end<br /><br /><br /><br />#==========================================<br />=begin<br />Comments on this post<br />bmarini posts on May 19, 2008 at 12:56<br />I took your partial validation idea and included it in AR Base<br />=end<br />module ActiveRecord<br /> module Validations<br /> module Partial<br /> def valid_for_attributes?( *attr_names )<br /> return validate_for_attributes(attr_names)<br /> end<br /> <br /> def validate_for_attributes( *attr_names )<br /> attr_names.map! {|a| a.to_s }<br /> <br /> unless valid?<br /> our_errors = Array.new<br /> errors.each { |attr,error|<br /> if attr_names.include? attr<br /> our_errors << [attr,error]<br /> end<br /> }<br /> <br /> errors.clear<br /> our_errors.each { |attr,error| errors.add(attr,error) }<br /> return false unless errors.empty?<br /> end<br /> return true<br /> end<br /> <br /> end<br /> end<br />end<br /><br />ActiveRecord::Base.class_eval do<br /> include ActiveRecord::Validations::Partial<br />end<br /><br />=begin<br />cgunther posts on Jul 02, 2009 at 11:40<br />bmarini, I think there is a bug in your code.<br /><br />When I called:<br />=end<br />@order.valid_for_attributes? 'first_name', 'last_name'<br /><br />=begin<br />It always passed validation. The error was the asterisk in front of attr_names on validate_for_attributes. It was being passed an array as the first attribute and when you run map! on it, it was converting the entire array to a single string (first_namelast_name).<br /><br />Here's the code that works for me:<br />=end<br />module ActiveRecord<br /> module Validations<br /> module Partial<br /> def valid_for_attributes?( *attr_names )<br /> return validate_for_attributes(attr_names)<br /> end<br /> <br /> def validate_for_attributes( attr_names )<br /> attr_names.map! {|a| a.to_s }<br /> <br /> unless valid?<br /> our_errors = Array.new<br /> errors.each { |attr,error|<br /> if attr_names.include? attr<br /> our_errors << [attr,error]<br /> end<br /> }<br /> <br /> errors.clear<br /> our_errors.each { |attr,error| errors.add(attr,error) }<br /> return false unless errors.empty?<br /> end<br /> return true<br /> end<br /> <br /> end<br /> end<br />end<br /><br />ActiveRecord::Base.class_eval do<br /> include ActiveRecord::Validations::Partial<br />end<br /><br /><br />#The main difference is that if you call validate_for_attributes directly, you have to pass it an array.<br /><br />#Chris Gunther
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
Tweet
t
沒有留言:
張貼留言