Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 表单提交后参数为空_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 表单提交后参数为空

Ruby on rails 表单提交后参数为空,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我有一个from,用户可以在这里更改密码。目前,我还没有做任何关于更新密码的事情,因为我的表单由于某种原因没有传递参数。在此之前,我给您提供了代码,只是为了让您知道,您将看到put内部命令用于调试 密码格式: <div class="title">Change Password</div> <div class="password"> <%= profile_error_messages_for(@profile) %> <%= f

我有一个from,用户可以在这里更改密码。目前,我还没有做任何关于更新密码的事情,因为我的表单由于某种原因没有传递参数。在此之前,我给您提供了代码,只是为了让您知道,您将看到
put
内部命令用于调试

密码格式:

<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>
用户模型:

  def password_match?(entered_password = '')
    puts 'Password: ' + entered_password.to_s
    puts salt
    password == User.hash_with_salt(entered_password, salt)
  end
在我尝试打印值的地方,它是空的。我们甚至不需要检查模型。因为当我在控制器中输入这个时,它也是空的

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

3天以来我一直在研究这个问题。请有人帮帮我。谢谢。

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

如果您不确定字段的存在或它在
params
返回的哈希中的位置,最好检查
params
本身

当您为助手使用
form_时,提交的字段将被包装在您为
传递的
form_的资源的小写模型名称(或简称名称)下。因此,在您的情况下,由于您已经给出了
:user
名称,因此
params
返回的哈希将包含一个键
“user”
,在该键下,您的所有字段都将以其给定的结构显示。因此,您应该使用:

params[:user][:current_password]

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

在Rails中调试: 我们使用调试工具,比如

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

例如:

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

      if @profile.password_match?(params[:current_password])
        binding.pry
      else
        binding.pry
        render 'edit'
      end
    end
当您发送HTTP请求时
binding.pry
停止执行,您可以在运行服务器的终端中访问
rails控制台

像这样的

[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)
在此之后,我希望您可以自己调试应用程序。
有关更多信息,请参阅


我认为您应该尝试放置参数。检查它是否真的不存在。您是否有可能在控制器中过滤掉它们(您没有提供完整的控制器代码,所以我不知道)?如果使用强参数,则必须将希望前端传递给服务器的参数列入白名单@李梅金:我不知道。它实际上删除了[过滤]。好的,当我尝试这个时,我看到我所有的输出都在那里。但是为什么我在password\u match中传递它时它是nil?当您检查所有想要的字段都包装在
user
下时,您注意到了吗?:D.是的,应该使用
params[:user][:current\u password]
来访问嵌套散列中的值。
[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)