简体   繁体   中英

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. I have resources :passwords in my routes.rb file. I looked at my login form and even though it doesn't really access the model I still used form_for and it worked. 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.

Hope this helps.

Figured it out by studying my sessions_controller and associated view file.

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. So the important thing is the first argument passed to the form_for helper.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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