简体   繁体   English

如何在rails上的ruby中将此form_tag表单转换为form_for表单?

[英]How can I convert this form_tag form into a form_for form in ruby on rails?

I've been trying to figure out how to write a form_for version of this form for my passwords_controller. 我一直在尝试找出如何为我的passwords_controller编写此表单的form_for版本。 I have resources :passwords in my routes.rb file. 我的route.rb文件中有资源:passwords。 I looked at my login form and even though it doesn't really access the model I still used form_for and it worked. 我查看了我的登录表单,尽管它实际上并没有访问我仍然使用form_for的模型,但仍然有效。 I tried the same thing and I keep getting issues so I'm using the code below and that's working but would really like to know what I'm doing wrong? 我尝试了同样的事情,但仍然遇到问题,因此我正在使用下面的代码,并且可以正常工作,但真的想知道我在做什么错吗?

<%= form_tag passwords_path, :method => :post do %>
  <div id="#emailFieldJoin">
    <%= text_field_tag :email, params[:email], :placeholder => "Email" %>
  </div>
  <div class="actions"><%= submit_tag "Reset Password" %></div>
<% end %>

Something like: 就像是:

<%= form_for @password do |f| %>
  <div id="#emailFieldJoin">
    <%= f.text_field :email %>
  </div>
  <div class="actions"><%= f.submit "Reset Password" %></div>
<% end %>

So in your controller you should create a @password object, probably fill the email from the params and then render this form. 因此,在您的控制器中,您应该创建一个@password对象,可能会填充来自paramsemail ,然后呈现此表单。

Hope this helps. 希望这可以帮助。

Figured it out by studying my sessions_controller and associated view file. 通过研究我的sessions_controller和关联的视图文件,可以找到答案。

Basically: 基本上:

  <%= form_for :password_reset, :url => passwords_path do |f| %>
  <%= f.text_field :email, :placeholder => "Email" %>      
  <%= f.submit "Reset Password", :disable_with => 'Log In'%>
  <% end %>

The first argument is a symbol of your choice. 第一个参数是您选择的象征。

I used params[:password_reset][:email] to grab these params from my view in my passwords controller. 我使用params [:password_reset] [:email]从我的密码控制器中的视图中获取这些参数。 So the important thing is the first argument passed to the form_for helper. 因此重要的是传递给form_for帮助器的第一个参数。

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

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