在表单提交后,Params是空的。

[英]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.

我有一个,用户可以在那里更改他们的密码。目前,我还没有更新密码,因为我的表单由于某些原因没有传递参数。在此之前,我给了您代码,只是为了让您知道,您将看到用于调试的put inside命令。

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:

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.

我从三天以来一直在研究这个问题。请帮帮我。谢谢你!

3 个解决方案

#1


1  

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.

如果您不确定某个字段的存在或它位于params返回的散列中的位置,那么检查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 :

当您使用form_for helper时,提交时的字段将在传递给form_for的资源的小写模型名称(或简单的名称)下包装。因此,在您的例子中,由于您已经给出了:user name, params返回的散列将包含一个键“user”,在这个键下,您的所有字段将以其给定的结构显示。所以你应该使用:

params[:user][:current_password]

#2


1  

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

尝试在控制器中使用params[:user][:current_password]。

#3


1  

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

@cyonder,由于您是Rails/Ruby的新手,我们的责任是至少给您一些可能对您有所帮助的想法:

Debugging in Rails:

We use debugging tools like pry, bye-bug

我们使用像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.

使用这些工具(pry),您可以在代码中放置断点,这样服务器执行就会停止,您可以分析对象在特定执行点的值。

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

发送HTTP请求时;绑定。pry停止执行,您可以访问正在运行服务器rails s的终端中的rails控制台

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

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

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/10/07/1b2873b4cfe6a1626e516a18938392b8.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告