Re: Form trying to save null values?

Jodi Showers
Thu, 22 Feb 2007 17:23:17 -0000

On 22-Feb-07, at 12:12 PM, ryan.bentz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
wrote:

>
> Okay...sorry to bug y''all, but I think I just figured out. I had
> specified attr_accessor :email, :location in my RetailUser model.
> Apparently that was screwing things up so I need to figure out why
> exactly.
>
> Thanks for your time!
>
> ryan
>
> On Feb 22, 8:47 am, "Ryan Bentz"
<ryan.be...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>> Hey there...new to the group but looks like there''s some great
>> discussion
>> going on throughout!
>>
>> I''m working on a sign up form right now that takes just an
email
>> and zip
>> code. It appears that the email and zip code make it as far as the
>> SELECT
>> statements when attempting to save the new user, but when it tries
>> to save
>> said values to their respective columns in the DB the log shows
>> VALUES(null,
>> null). My code looks like this:
>>
>> profile controller:
>> def rSignIn
>>     @retailUser = RetailUser.new(params[:user])
>>     if request.post?
>>       if @retailUser.save
>>         session[:user] = RetailUser.authenticate(@retailUser.email, @
>> retailUser.location)
>>         flash[:message] = "Signup successful"
>>         redirect_to :controller => "browse", :action =>
"retailHome"
>>       else
>>         flash[:warning] = "Signup failed"
>>       end
>>     end
>>   end
>>
>> retail user model:
>> validates_length_of :email, :within => 6..65
>>   validates_length_of :location, :is => 5
>>   validates_presence_of :email, :location
>>   validates_uniqueness_of :email
>>   validates_format_of :email, :with =>
>> /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message =>
"Invalid
>> email"
>>
>>   attr_protected :id
>>
>>   attr_accessor :email, :location
>>
>>   def self.authenticate(email, location)
>>     u = find(:first, :conditions =>
["user[email]=?",email])
>>     return nil if u.nil?
>>     return u
>>     nil
>>   end
>>
>> sign in view:
>> <% form_for :user, @retailUser, :html => {:id =>
>> "retailSignInForm"}, :url
>> => { :controller => "profile", :action =>
"rSignIn"} do |f| %>
>>
>>             <label
for="retailSignInText">Email: <%>>
f.text_field:email, :id => "retailSignInText", :class=>
"text" %>
>>
>>
>> Zip
code: <%>> f.text_field:location, :id =>
"retailSignInZip", :class=> "text" %>
>>
>>
>>             <%= button_to "Sign In", :controller =>
>> "profile", :action =>
>> "rSignIn" %>
>>
>>     <% end %>
>>
>> Any clue as to what I''m doing wrong?

Welcome Ryan,

If you had legitimate fields in retail_user model, then
attr_accessor :email, :location would have overloading default rails
accessors - put another way,  you don''t need to create attributes -
rails inspects the table and creates accessors for you.

++ id is protected from mass assignment, so attr_protected isn''t
needed for this attribute

Cheers,
Jodi
General Partner
The nNovation Group inc.