如何使用邪恶进行验证?

时间:2015-05-21 12:38:15

标签: ruby-on-rails ruby-on-rails-3 wicked-pdf

我有一个users_controller和一个user_steps_controller,它有三个步骤:business,:payment和:login

在user.rb模型中

class User < ActiveRecord::Base
validates_presence_of :fname, :lname, :email, :mob, :country, :state, :suburb, :postal ,:add
end

在检查验证时如果我放了一些随机值,那么它也会给出错误

Fname can't be blank
Lname can't be blank
Email can't be blank
Mob can't be blank
Country can't be blank
State can't be blank
Suburb can't be blank
Postal can't be blank
Add can't be blank

请帮帮我

这是我的users_controller

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:id])
    if @user.save
      session[:user_id]= @user.id
      @user.update_attributes(user_params )
      redirect_to user_steps_path
    else
      render :new
    end
  end

  private
  def user_params
    params.require(:user).permit( :fname, :lname, :email, :mob, :gender_male, :gender_female, :country, :state, :suburb, :postal ,:add, :cmpyname, :abnacn, :cmpyadd, :cmpydet,:cash, :paypal,:bsb,:usrname,:password_hash, :password_salt, :selcat, :protit, :prodes)
  end

user.rb

class User&lt;的ActiveRecord ::基

validates :fname, :lname, :email, :mob, :country, :state, :suburb, :postal ,:add, :presence => true

attr_accessor :current_step

validates_presence_of :cmpyname, :abnacn, :cmpyadd, :cmpydet, if: -> { current_step?(:business) }
validates_presence_of :usrname,:password_hash, :password_salt, :selcat, :protit, :prodes, if: -> { current_step?(:login) }

def current_step?(step_key)
  current_step == step_key
end

end

user_steps_controller

class UserStepsController < ApplicationController
  include Wicked::Wizard
  steps :business, :login, :payment

def show
  @user = current_user
  render_wizard
end

  def update

    @user = current_user
    params[:user][:current_step] = step
    @user.update_attributes(user_params )
    render_wizard @user

  end

 private
  def user_params
    params.require(:user).permit( :cmpyname, :abnacn, :cmpyadd, :cmpydet,:cash, :paypal,:bsb,:usrname,:password_hash, :password_salt, :selcat, :protit, :prodes)
  end
end

2 个答案:

答案 0 :(得分:2)

user.rb

class UserStepsController < ApplicationController
  include Wicked::Wizard
  steps :personal, :social

  def show
    @user = current_user
    render_wizard
  end

  def update
    @user = current_user
    params[:user][:current_step] = step
    @user.attributes = user_params
    render_wizard @user
  end

private

  def redirect_to_finish_wizard(options = nil)
    redirect_to root_url, notice: "Thank you for signing up."
  end

  def user_params
    params.require(:user).permit(:name, :current_step, :date_of_birth, :bio, :twitter_username, :github_username, :website)
  end
end

user_steps_controller.rb

protected void btnsubmit_Click(object sender, EventArgs e)
{
    Ticket_MailTableAdapters.tbl_TicketTableAdapter tc;
    tc = new Ticket_MailTableAdapters.tbl_TicketTableAdapter();
    DataTable dt = new DataTable();
    dt = tc.GetEmail(dpl_cate.SelectedValue);
    foreach (DataRow row in dt.Rows)
    {
        string eml = (row["Emp_Email"].ToString());
        var fromAddress = "emailAddress";
        var toAddress = eml;
        const string fromPassword = "*****";
        string body = "Welcome..";
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
               {
                 smtp.Host = "outlook.office365.com";
                 smtp.Port = 993;
                 smtp.EnableSsl = true;

                 smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                 smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
                 smtp.UseDefaultCredentials = false;
                 smtp.Timeout = 600000;
               }
        // Passing values to smtp object
         smtp.Send(fromAddress, toAddress, subject, body);
     }
}

此方法适用于您。

答案 1 :(得分:1)

你真正的问题在于:

@user = User.new(params[:id])

我假设params[:id]nil,否则会失败。基本上,您实例化User没有提供数据并尝试保存它。很明显,您提供的验证将失败。如果您实际提交的是包含用户数据的表单,则需要通过user_params,您已经定义如下:

@user = User.new(user_params)

如果您需要在不同的步骤中对用户模型进行验证,则需要根据表单的状态有条件地运行验证:

class User 
  attr_accessor :current_step
  validates_presence_of :business_related_attr, if: -> { current_step?(:business) }

  def current_step?(step_key)

    current_step.blank? || current_step == step_key
  end
end