LoginSignup
0
0

More than 3 years have passed since last update.

【Rails】ルーティングを名前空間でグループ分けしている場合のform_withの記法【Ruby】

Posted at

はじめに

ヘルパーメソッドのform_withを使用して入力フォームを作ろうとしていたところ、NoMethodErrorが出てしまい詰まっていました。
スクリーンショット 2021-03-05 21.50.46.png

結論

route.rbにおいてルーティングを名前空間shopsでグループ分けしていたにもかかわらず、shopsをform_withの引数に渡していなかった。

コード

route.rb
namespace :shops do
    resources :posts  
---中略---
end
shops/posts_controller.rb
class Shops::PostsController < ApplicationController
  --- 中略 ---
  def new
    @post = Post.new
  end
  --- 中略 ---
end

shops/post/new.html.erb
<%= form_with model:[:shops,@post], local: true do |f| %>
      --- 中略 ---
    <div>
      <%= f.submit "送信"%>
    </div>
<% end %>

resourcesをネストしているときに親モデルを引数に渡すのと同じ感覚です。

参考

https://techracho.bpsinc.jp/ohno/2019_12_10/84349
https://railsdoc.com/page/form_with

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