I have a simple signup form:
<h1>Signup as a new user</h1>
<%= form_for(@user) do |f| %>
<p>
<%= f.label :username %>
<%= f.text_field :username %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :password %>
</p>
<p>
<%= f.submit "Save" %>
</p>
<% end %>
That redirects to the create method of the UsersController:
def create
password = params[:password].crypt("$1$}")
@user = User.new(:username => params[:username], :password => password)
@user.save
flash[:message] = "User #{User.username} created!"
redirect_to user_path(@user)
end
But this throws an error undefined method crypt' for nil:NilClass
. Why would the password be Nil? I checked the HTML params and got this:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"aAcaURTLKfwmXULEUzLX36tZAKER/kMxKKOROOXgoU8=", "user"=>{"username"=>"Chris", "password"=>"[FILTERED]"}, "commit"=>"Save"}
Does "[FILTERED}"
mean it isn't a string? How can I access it from params
?
Its not nil, it just exists inside of :user, so try
params[:user][:password].crypt("$1$")
In fact all of your params work here is incorrect, all the fields are scoped by :user, this is one of the things that form_for does for you automatically.
So to get the username, you'd want to say params[:user][:username]
.