简体   繁体   English

使用 rails 5 传递隐藏字段标签参数

[英]Pass hidden field tag params with rails 5

From the views/plans/new.html.erb I get the plan_id and price params with the following:views/plans/new.html.erb我得到 plan_id 和 price 参数如下:

<%= link_to "Sign up", new_store_registration_path(:plan_id => plan.id, :price => plan.price) %>

Then the app redirects to the sign-up page and with the following I keep the previous params and add the email as well:然后应用程序重定向到注册页面,并使用以下内容保留以前的参数并添加电子邮件:

registrations_controller.rb registrations_controller.rb

def after_sign_up_path_for(resource)
  new_transaction_path(session[:registration_params].merge(ema‌​il: resource.email))
end

def after_inactive_sign_up_path_for(resource)
  new_transaction_path(session[:registration_params].merge(ema‌​il: resource.email))
end

Finally after sign-up, the app redirects to the views/transcation/new.html.erb, which has the plan_id , price and email params.最后注册后,应用程序重定向到 views/transcation/new.html.erb,其中包含plan_idpriceemail参数。

Parameters: {"ema‌​il"=>"example@gmail.com", "plan_id"=>"bs96", "price"=>"150.0"}

At this point I'm trying to pass the email param to the transaction with <%= hidden_field_tag(:email, params["email"]) %>在这一点上,我试图将电子邮件参数传递给<%= hidden_field_tag(:email, params["email"]) %>

But not getting an email as you can see in the following:但是没有收到电子邮件,如下所示:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"KeS2xK7NIJZwFQvW2kJKupcpURnQweq+yoRgk9AJ1aaOgFIIym4RKadI4jc6vYynMo4vKR4eLmdIynfBG+EusQ==", "email"=>"", "plan_id"=>"bs96", "amount"=>"150.0", "payment_method_nonce"=>"0c22f2fa-e212-0ad3-753b-0d183d02522b"}

Any ideas on what I'm doing wrong???关于我做错了什么的任何想法???

Update1更新1

Inside the views/transcation/new.html.erb there is the braintree drop in ui and the script along with the three hidden fields:在 views/transcation/new.html.erb 中有 ui 中的 Braintree 下拉和脚本以及三个隐藏字段:

<div class="form-container radius-box glassy-bg small-10 small-centered medium-8 large-6 columns">
  <%= form_tag transactions_path do%>
      <div id="dropin"></div>
      <%= hidden_field_tag(:email, params["email"]) %>
      <%= hidden_field_tag(:plan_id, params["plan_id"]) %>
      <%= hidden_field_tag(:amount, params["price"]) %>
      <%=submit_tag "Pay #{params["price"]}$", class: "button mt1" %>
  <%end%>
</div>

<script>
    braintree.setup("<%=@client_token%>", 'dropin', {
        container: 'dropin'
    });
</script>

Please set the value of hidden field like:请设置隐藏字段的值,如:

<%= f.hidden_field :email, :value => "test@test.com" %>

or

<%= f.hidden_field :email, :value => @object.email %>

I hope you have not confused.我希望你没有混淆。

<%= hidden_field_tag "email", params[:email] %>

hidden_field_tag used with form_tag hidden_field_tagform_tag hidden_field_tag使用

<%= f.hidden_field "email", params[:email] %>

and f.hidden_field is used with form_forf.hidden_fieldform_for一起使用

There are Two tags for add Hidden fields in ruby on rails view有两个标签用于在 ruby​​ on rails 视图中添加隐藏字段

  1. hidden_field 隐藏字段
  2. hidden_field_tag hidden_​​field_tag

Form for rails view with hidden fields带有隐藏字段的 rails 视图表单

<% form_for(:request, :url => requests_path) do |f| %>

`<div class="actions">`
    <%= f.hidden_field :column_name %>
    <%= hidden_field_tag 'selected', 'column_name' %>
    <%= f.submit e %>
</div>

<% end %>` <% 结束 %>`

Controller code:控制器代码:

 params[:selected]="column_name"
 params[:request][:column_name] = request.column_name

When we used f.hidden_field:当我们使用 f.hidden_​​field 时:

<%= f.hidden_field :column_name %>

it change to html:它更改为 html:

<input type="hidden" id="request_column_name" name="request[column_name]" value="#{@request.column_name}" />

and when we used hidden_field_tag当我们使用 hidden_​​field_tag

<%= hidden_field_tag 'selected', 'column_name' %>

it change to html:它更改为 html:

<input id="selected" name="selected" type="hidden" value="none"/>

And we can send a custom value as a hidden input for your model like that:我们可以发送一个自定义值作为您模型的隐藏输入,如下所示:

<%= f.hidden_field :model_field_name, value: 12 %>

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

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