Disclaimer: super new to rails. I’m using Rails 3.2
Anyways, I’m trying to create a form for Merchants to sign in. I have a MerchantSessionsController that tries to create a new session based on signin form input:
This is what I have in my
app/views/merchant_sessions/new.html.erb
<%= form_for(:merchant_session, :url => merchant_sessions_path) do |f| %>
<div class="field">
<%= f.label :userName %><br />
<%= f.text_field :userName %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="actions">
<%= f.submit "Sign in" %>
</div>
<% end %>
The file app\controllers\merchant_sessions_controller.rb contains:
def create
merchant = Merchant.find_by_userName(params[:userName])
if merchant && merchant.authenticate(params[:password])
merchant_session[:merchant_id] = merchant.id
redirect_to root_url, :notice => "Merchanthas been logged in"
else
flash.now[:error] = "Invalid username or password."
@title = "Merchant Signin"
render "new"
end
end
Unfortunately, the params[:userName] and params[:password] keep getting passed as nil, even though on the debug output on the merchants signin page, I see that the userName and password are definitely being passed in.
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
authenticity_token: 8WsOviJyY1kktPq9dDO+OFePdSKf2uGLY3Pnc4bU2tc=
merchant_session: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
userName: asd
password: ddsad
commit: Sign in
action: create
controller: merchant_sessions
I’ve also attempted to access the params[:action] parameter, which worked fine. Why is it that the userName and password parameters are nil? I had changed the name of the MerchantSessionsController (formerly just SessionsController), but I don’t think that should be the problem
You’re looking in the wrong place for the username and password, note the specific structure of your YAML dump:
and your form:
You want to look at
params[:merchant_session][:userName]andparams[:merchant_session][:password]instead ofparams[:userName]andparams[:password]: