0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

form_with内にbutton_toを入れるとmethodが変わる

Last updated at Posted at 2021-03-12

####【ページ内リンク】
0.環境
1.事象
2.解決策
①button_toをform_withの外に出す
②button_toをlink_toに変える

#0.環境
・AWS
・Rails 5.2.4.5
・ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
・MySQL 5.7.31

#1.事象
下記において、更新ボタンを押したら@team削除された!
削除ボタンを押しても勿論削除される

solution1.html.erb
<%= form_with model: @team do |f| %>            
  <%= f.text_field :name %>
  <%= f.submit "更新" %>
  <%= button_to "削除", team_path(key: @team.id), method: :delete %>
<% end %>

ページ内リンクへ戻る

#2.解決策
##①button_toをform_withの外に出す

solution2.html.erb
<%= form_with model: @team do |f| %>            
  <%= f.text_field :name %>
  <%= f.submit "更新" %>
<% end %>
<%= button_to "削除", team_path(key: @team.id), method: :delete %>

ページ内リンクへ戻る

##②button_toをlink_toに変える

example.html.erb
<%= form_with model: @team do |f| %>            
  <%= f.text_field :name %>
  <%= f.submit "更新" %>
  <%= link_to "削除", team_path(key: @team.id), method: :delete %>
<% end %>

ページ内リンクへ戻る

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?