Forum: Ruby on Rails Form trying to save null values?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C2e626d51b252b0593e4b62499ae1f96?d=identicon&s=25 Ryan Bentz (Guest)
on 2007-02-22 17:51
(Received via mailing list)
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| %>
        <div class="signInFormLine">
            <label for="retailSignInText">Email:</label> <%=
f.text_field:email, :id => "retailSignInText", :class=> "text" %>
        </div>
        <div class="signInFormLine">
            <label for="retailSignInZip">Zip code:</label> <%=
f.text_field:location, :id => "retailSignInZip", :class=> "text" %>
        </div>
        <div class="signInFormLine">
            <%= button_to "Sign In", :controller => "profile", :action
=>
"rSignIn" %>
        </div>
    <% end %>

Any clue as to what I'm doing wrong?
C2e626d51b252b0593e4b62499ae1f96?d=identicon&s=25 ryan.bentz@gmail.com (Guest)
on 2007-02-22 18:14
(Received via mailing list)
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
B09a3f6cdc4797532647d2d264b5df49?d=identicon&s=25 Jodi Showers (jshow)
on 2007-02-22 18:26
(Received via mailing list)
On 22-Feb-07, at 12:12 PM, ryan.bentz@gmail.com wrote:

> On Feb 22, 8:47 am, "Ryan Bentz" <ryan.be...@gmail.com> wrote:
>> said values to their respective columns in the DB the log shows
>>         flash[:message] = "Signup successful"
>>   validates_presence_of :email, :location
>>     u = find(:first, :conditions => ["user[email]=?",email])
>>             <label for="retailSignInText">Email:</label> <%=
>>         </div>
>>     <% 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.
www.nnovation.ca/blog
This topic is locked and can not be replied to.