表单提交后参数为空 [英] Params are empty after form submission

查看:46
本文介绍了表单提交后参数为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 from,用户可以在那里更改他们的密码.目前,我还没有做任何关于更新密码的事情,因为我的表单出于某种原因没有传递参数.之前,我给你代码,只是为了让你知道,你会在命令中看到 puts 用于调试目的.

密码形式:

更改密码

<div class="密码"><%= 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, '当前密码') %><%= f.password_field :current_password, class: 'form-control input-md input-width-large' %>

<div class="input-box"><%= label_tag(:password, '新密码') %><%= f.password_field :password, class: 'form-control input-md input-width-large' %>

<div class="input-box"><%= label_tag(:password_confirmation, '密码确认') %><%= f.password_field :password_confirmation, class: 'form-control input-md input-width-large' %>

<%= submit_tag '更改密码',类:'btn btn-success btn-md input-width-large' %><%结束%>

profiles_controller:

elsif params[:attribute] == '密码'puts 'params:' + params[:current_password].to_s把这里 1"如果@profile.password_match?(params[:current_password])把这里 2"别的把这里 3"渲染编辑"结尾结尾

用户模型:

 def password_match?(entered_pa​​ssword = '')puts '密码:' + enter_password.to_s放盐密码 == User.hash_with_salt(entered_pa​​ssword, salt)结尾

我曾经尝试打印出值的地方,它是空的.我们甚至不需要检查模型.因为当我在控制器中输入时,它也是空的.

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

我从 3 天起就一直在研究这个问题.请有人帮助我=).谢谢.

解决方案

如果您不确定某个字段是否存在或其在返回的散列中的位置,最好检查 params 本身通过 params.

当您使用 form_for 助手时,提交时的字段将被包裹在您为 form_for 传入的资源的小写模型名称(或简称为名称)下.因此,在您的情况下,由于您提供了 :user 名称,params 返回的哈希将包含一个键 'user',在该键下所有您的字段将以其给定的结构呈现.所以你应该使用:

params[:user][:current_password]

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.

解决方案

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]

这篇关于表单提交后参数为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆