NoMethodErrorを解決したいです
商品登録と、レビュー機能を実装しているのですが
商品登録用のフォームを作る際に、以下のエラーが発生しております
発生している問題・エラーメッセージ
Showing /Users/fujiitomohiro/environment/MyApp/app/views/s_dogfoods/new.html.erb where line #3 raised: undefined method `dogfoods_path' for #<#<Class:0x00007fb08503ee58>:0x00007fb09fe877c0> Did you mean? s_dogfoods_path s_dogfood_path Extracted source (around line #3): <% provide(:title, 'ドッグフード追加') %> <h2>ドッグフード登録</h2> <%= form_with(model: @dogfood, local: true) do |f| %> <div class="form-box"> <%= f.label :name %> <%= f.text_field :name, :placeholder => "商品名" %>
該当のソースコード
(s_dogfoods_controller.rb) class SDogfoodsController < ApplicationController before_action :set_dogfood, only: %i[ show edit ] def index @dogfoods = Dogfood.all end def show @review = Review.new @reviews = @dogfood.reviews end def new @dogfood = Dogfood.new end def create @dogfood = Dogfood.new(dogfood_params) if @dogfood.save redirect_to s_dogfood_url(@dogfood) else render 'new' end end private def set_dogfood @dogfood = Dogfood.find(params[:id]) end def dogfood_params params.require(:dogfood).permit(:name, :protein, :fal, :price) end end
(new.html.erb) <% provide(:title, 'ドッグフード追加') %> <h2>ドッグフード登録</h2> <%= form_with(model: @dogfood, local: true) do |f| %> <div class="form-box"> <%= f.label :name %> <%= f.text_field :name, :placeholder => "商品名" %> </div> <div class="form-box"> <%= f.label :protein %> <%= f.text_field :protein, :placeholder => "タンパク量" %> </div> <div class="form-box"> <%= f.label :fal %> <%= f.text_field :fal, :placeholder => "脂肪量" %> </div> <div class="form-box-last"> <%= f.label :price %> <%= f.text_field :price, :placeholder => "商品価格" %> </div> <%= f.submit "登録", class: "dogfood-submit" %> <% end %>
(dogfood.rb) class Dogfood < ApplicationRecord has_many :reviews validates :name, presence: true, length: {maximum: 20}, uniqueness: true validates :protein, presence: true, numericality: { less_than: 100 } validates :fal, presence: true, numericality: { less_than: 100 } validates :price, presence: true, numericality: { less_than: 100000 } end
(review.rb) class Review < ApplicationRecord belongs_to :dogfood end
(review_controller.rb) class ReviewsController < ApplicationController def create @review = Review.new(review_params) @review.save redirect_to s_dogfood_path(@review.dogfood) end private def review_params params.require(:review).permit(:dogfood_id, :body) end end
(royte.rb) Rails.application.routes.draw do get 'password_resets/new' get 'password_resets/edit' root to: 'home#index' get '/login', to: 'sessions#new' post '/login', to: 'sessions#create' delete '/logout', to: 'sessions#destroy' get '/signup', to: 'users#new' get '/registration', to: 's_dogfoods#new' resources :users resources :s_dogfoods resources :reviews resources :account_activations, only: [:edit] resources :password_resets, only: [:new, :create, :edit, :update] namespace :api, {format: 'json'} do namespace :v1 do resources :dogfoods, only: [:index, :show] end end end
試したこと
form_widhにurlを設定することで、商品登録はできるようになったのですが
次は、render 'new'が上手く動作せずにエラーが発生しました
newアクションの設定がおかしいのかな?と思って調べてみたのですが
解決することができなかったので、質問させていただきました
補足情報
Rails 6.0.3.5で作成しています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/21 12:25