0
1

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.

フォームの遷移先の指定方法

Posted at

Railsでフォームの遷移先が思ったようにいかなく、つまずいたため備忘録も兼ねて書かせていただきます。

##問題点

編集データをcarts#updateに届けく、以下のようにしてみたが

carts/edit.html.erb
<%= form_with(model: @cart, local: true ) do |f| %>
  <div>
    <%= f.label :quantity, "購入数" %>
    <%= f.number_field :quantity %>
  </div>
  <%= f.submit "更新", class: "btn" %>
<% end %>

この遷移先が、なぜかcart_product_pathになってしまう。
また、Did you mean? で言われている edit_product_pathも希望する遷移先とは違う。

スクリーンショット 2020-02-21 20.46.30.png

##改善方法

form_withのオプションで、url: cart_pathと指定するとうまく遷移するようになりました。

      carts_add   POST   /carts/add(.:format)        carts#add
      carts       GET    /carts(.:format)            carts#index
      edit_cart   GET    /carts/:id/edit(.:format)   carts#edit
      cart        PATCH  /carts/:id(.:format)        carts#update (#遷移先にしたい)
                  PUT    /carts/:id(.:format)        carts#update
                  DELETE /carts/:id(.:format)        carts#destroy
carts/edit.html.erb
<%= form_with(model: @cart, url: cart_path, local: true ) do |f| %>
  <div>
    <%= f.label :quantity, "購入数" %>
    <%= f.number_field :quantity %>
  </div>
  <%= f.submit "更新", class: "btn" %>
<% end %>
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?