开发者

Rails: validations not enforced, call manually?

开发者 https://www.devze.com 2023-03-01 01:37 出处:网络
I check for validates :group_id, :presence => true in my 开发者_如何学JAVAmodel \"project\". Yet when I create a project without a group_id, I don\'t get a validation-error. If I try to do

I check for

validates :group_id, :presence => true

in my 开发者_如何学JAVAmodel "project". Yet when I create a project without a group_id, I don't get a validation-error. If I try to do

p = Project(:name => "Test")
p.save

the rails console returns false, with save! it tells me the validation for group_id failed. So the validation is somehow performed on the save method, yet it isn't by default in my app. Do I have to do a manual

if @project.save == true
...
end

or something in my controller?

Thank you, B


You can check @project.valid? before save.

  def signup
    if request.post?
      @user_data = UserData.new(:login => params[:user], :password => params[:password])
      if @user_data.valid?
        if @user_data.save
          session[:cuser] = UserData.authenticate(@user_data.login, @user_data.password).login
          redirect_to(:controller=> 'sync', :action=> 'all')
          return
        end
      else
        @error_on_signup = true
      end
    end
0

精彩评论

暂无评论...
验证码 换一张
取 消