简体   繁体   English

为什么在password_reset的编辑操作中使用hidden_​​field_tag而不是f.hidden_​​field

[英]Why use hidden_field_tag instead of f.hidden_field in edit action of password_reset

I'm doing chap12: Password Reset in rails railstutorial.org and i saw this code app/views/password_resets/edit.html.erb 我正在做第12章:在rails railstutorial.org中重置密码,我看到了此代码app/views/password_resets/edit.html.erb

<h1>Reset password</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@user, url: password_reset_path(params[:id])) do |f| %>
      <%= render 'shared/error_messages' %>

      <%= hidden_field_tag :email, @user.email %>

      <%= f.label :password %>
      <%= f.password_field :password, class: 'form-control' %>

      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation, class: 'form-control' %>

      <%= f.submit "Update password", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

And PasswordResetsController class PasswordResetsController < ApplicationController PasswordResetsController类PasswordResetsController <ApplicationController

  def edit
    @user = User.find_by email: params[:email]
  end

  def update
    @user = User.find_by email: params[:email]
    if params[:user][:password].empty?
      add_errors
    elsif @user.update_attributes user_params
      log_in @user
      flash[:success] = t "controllers.password_reset.success"
      redirect_to @user
    else
      render :edit
    end
  end

end

Reset link like this: http://localhost:3000/password_resets/DCxf-6H4fyBf32QrW-d2Zg/edit?email=trag08%40gmail.com When click link, route call action edit and get email to params[:email] and view edit.html.erb and put email into hiddren field. 重置像这样的链接: http://localhost:3000/password_resets/DCxf-6H4fyBf32QrW-d2Zg/edit?email=trag08%40gmail.com单击链接时,路由呼叫操作edit并将电子邮件发送到params [:email]并查看edit.html.erb并将电子邮件放入“隐藏”字段。 And why in this code, they don't use f.hidden_field :email, @user.email . 以及为什么在此代码中,他们不使用f.hidden_field :email, @user.email

I try use f.hidden_field :email, @user.email and edit update action in controller like this. 我尝试使用f.hidden_field :email, @user.email并像这样在控制器中编辑更新操作。

def update
        @user = User.find_by email: params[:user][:email]
        if params[:user][:password].empty?

Rails rase error: undefined method merge' for "xxx@gmail.com":String`, and it's not get user. Rails rase错误:“ xxx@gmail.com”:String`的undefined method合并',并且没有得到用户。 Why like this? 为什么这样? .

As you see in the tutorials example <%= hidden_field_tag :email, @user.email %> is not creating a param that's available in params[:user] . 如您在教程示例<%= hidden_field_tag :email, @user.email %>所看到的<%= hidden_field_tag :email, @user.email %>并未创建在params[:user]可用的params[:user]

It's creating a param that's available in params[:email] 它正在创建可在params[:email]使用的param params[:email]

When they call the email they use: 当他们呼叫电子邮件时,他们使用:

@user = User.find_by email: params[:email]

You use: 你用:

@user = User.find_by email: params[:user][:email]


I would recommend looking into what exactly is created by a rails helper tag as the one you're using. 我建议您仔细研究一下Rails helper标签所创建的内容。 See Form Tag Helpers . 请参阅表单标签助手

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM