I’m new to rails and have a simple problem.
I have a rails model:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation, :description
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true
validates :name, :presence => true
validates :password, :presence => true
...
end
On my update page for this model I have a form containing a textbox for email and a textbox for name.
in my controller I have the following update method:
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(:name => params[:name], :email => params[:email])
flash[:success] = "Profile updated"
format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
format.xml { head :ok }
else
@title = "Edit user"
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
This is failing with a strange error message:
undefined method `downcase' for nil:NilClass
Can anybody tell me where I am going wrong here? I’m sure I’m doing something daft, but cannot work out what it is…
You can trigger validation conditionally if you insert some kind of helper method. This is commonly the case for multi-stage entries, or partial updates:
I really do hope you’re not saving the password as plain-text. This is a huge liability. Usually
passwordandpassword_confirmationare temporaryattr_accessormethods that are later hashed and saved.