cyonder
cyonder

Reputation: 872

Params are empty after form submission

I have a from, where users can change their passwords. For now, I haven't done anything about updating the password because my form doesn't pass params for some reason. Before, I give you the code, just to let you know, you will be seeing puts inside command for debugging purposes.

Password Form:

<div class="title">Change Password</div>
<div class="password">
  <%= profile_error_messages_for(@profile) %>

  <%= form_for :user, url: change_password_path do |f| %>
      <%= hidden_field_tag 'attribute', 'password', id: nil %>
      <div class="input-box">
        <%= label_tag(:current_password, 'Current Password') %>
        <%= f.password_field :current_password, class: 'form-control input-md input-width-large' %>
      </div>
      <div class="input-box">
        <%= label_tag(:password, 'New Password') %>
        <%= f.password_field :password, class: 'form-control input-md input-width-large' %>
      </div>
      <div class="input-box">
        <%= label_tag(:password_confirmation, 'Password Confirmation') %>
        <%= f.password_field :password_confirmation, class: 'form-control input-md input-width-large' %>
      </div>
      <%= submit_tag 'Change Password', class: 'btn btn-success btn-md input-width-large' %>
  <% end %>
</div>

profiles_controller:

elsif params[:attribute] == 'password'
  puts 'params: ' + params[:current_password].to_s

  puts 'here 1'
  if @profile.password_match?(params[:current_password])
    puts 'here 2'
  else
    puts 'here 3'
    render 'edit'
  end
end

User model:

  def password_match?(entered_password = '')
    puts 'Password: ' + entered_password.to_s
    puts salt
    password == User.hash_with_salt(entered_password, salt)
  end

where ever I tried printing out value, it is empty. we don't even need to check model. because when I typed this in controller, it is also empty.

puts 'params: ' + params[:current_password].to_s

I have been working on this problem since 3 days. Please somebody help me =). Thank you.

Upvotes: 3

Views: 3101

Answers (3)

Shiva
Shiva

Reputation: 12592

@cyonder, since you are new to Rails/Ruby its our responsibility to at least give you some idea that may help you:

Debugging in Rails:

We use debugging tools like pry, bye-bug

Using these tools (pry) you can put break-points in code so that the server execution stops at that point and you can analyse the object's value in that particular point of execution.

For Example:

elsif params[:attribute] == 'password'
      binding.pry

      if @profile.password_match?(params[:current_password])
        binding.pry
      else
        binding.pry
        render 'edit'
      end
    end

When you send HTTP request; binding.pry stops execution and you can get access to rails-console in the terminal you are running server rails s

Something like this

[1, 10] in /PathTo/project/app/controllers/articles_controller.rb
    3:
    4:   # GET /articles
    5:   # GET /articles.json
    6:   def index
    7:     binding.pry
=>  8:     @articles = Article.find_recent
    9:
   10:     respond_to do |format|
   11:       format.html # index.html.erb
   12:       format.json { render json: @articles }

(pry)

After this I hope you can debug your app your self.
For more info see

http://guides.rubyonrails.org/debugging_rails_applications.html#debug http://railscasts.com/episodes/280-pry-with-rails

Upvotes: 1

limekin
limekin

Reputation: 1944

It's much better to inspect params itself if you are unsure about the presence of a field or where it is located in the hash returned by params.

When you use form_for helper, the fields when submitted will be wrapped under the lowercase model-name of the resource (or simply the name) you pass in for form_for. So in your case, since you have given the :user name, the hash returned by params will contain a key 'user' under which all of your fields will be present with their given structure. So you should use :

params[:user][:current_password]

Upvotes: 1

Sir_Fleetwood
Sir_Fleetwood

Reputation: 46

Try using params[:user][:current_password] in your controller.

Upvotes: 1

Related Questions