・解決したいこと
イベント投稿のアプリを作っているのですが、新規投稿しようとするとタグのパスに移動しようとしてエラーが起きてしまいます。
・エラー文
ruby
1NoMethodError in Events#create 2Showing /Users/user/projects/asomemo/app/views/events/_form.html.erb where line #1 raised: 3 4undefined method `events_tags_path' for #<#<Class:0x00007fbbf5c2b038>:0x00007fbbf5c29738> 5Did you mean? events_path 6Extracted source (around line #1): 7 8<%= form_with(model: @event, local: true) do |f|%> 9 <%= render 'shared/error_messages', model: f.object %> 10 <div class="field"> 11 <label class="label">イベント名</label> 12 <%= f.text_field :name, class: :form_control %> 13 </div> 14 15```new.html 16```ここに言語を入力 17コード<div class="main"> 18 <div class="inner"> 19 <div class="form__wrapper"> 20 <span class="page-heading"> 21 新規投稿 22 </span> 23 <%= render partial: "form", locals: { event: @event } %> 24 <%# 部分テンプレートでフォームを表示する %> 25 </div> 26 </div> 27</div>
form.html(該当コード)
ruby
1<%= form_with(model: @event, local: true) do |f|%> 2 <%= render 'shared/error_messages', model: f.object %> 3 <div class="field"> 4route 5 6```route 7```ruby 8Rails.application.routes.draw do 9 devise_for :users 10 root to: "events#index" 11 resources :events, only: [:index, :new, :create, :show, :destroy, :edit, :update] do 12 collection do 13 get 'search' 14 end 15 end 16end
events_controller
events_controller
1```ruby 2class EventsController < ApplicationController 3 def index 4 @events = Event.all 5 end 6 7 def new 8 @event = EventsTag.new 9 end 10 11 def create 12 @event = EventsTag.new(event_params) 13 if @event.valid? 14 @event.save 15 redirect_to root_path 16 else 17 render :new 18 end 19 end 20 21 def search 22 return nil if params[:keyword] == "" 23 tag = Tag.where(['tagname LIKE ?', "%#{params[:keyword]}%"] ) 24 render json:{ keyword: tag } 25 end 26 27 def show 28 @event = Event.find(params[:id]) 29 end 30 31 def edit 32 @event = Event.find(params[:id]) 33 unless @event.user_id == current_user.id 34 redirect_to action: :index 35 end 36 end 37 38 def update 39 @event = Event.find(params[:id]) 40 if @event.update(event_params) 41 redirect_to event_path 42 else 43 render :show 44 end 45 end 46 47 def destroy 48 @event = Event.find(params[:id]) 49 if @event.destroy 50 redirect_to root_path 51 end 52 end 53 54 private 55 56 def event_params 57 params.require(:events_tag).permit(:name, :explanation, :facility_id, :scale_id, :category_id, :volunteer, :tagname, images: []).merge(user_id: current_user.id) 58 end 59end 60```_event_model 61```ruby 62class Event < ApplicationRecord 63 extend ActiveHash::Associations::ActiveRecordExtensions 64 belongs_to :user 65 has_many_attached :images 66 belongs_to :facility 67 belongs_to :scale 68 belongs_to :category 69 has_many :event_tag_relations, dependent: :destroy 70 has_many :tags, through: :event_tag_relations, dependent: :destroy 71end 72```event_tag.model 73```ruby 74class EventsTag 75 76 include ActiveModel::Model 77 attr_accessor :name, :explanation, :facility_id, :scale_id, :category_id, :volunteer, :tagname, :user_id 78 79 80 with_options presence: true do 81 validates :name 82 validates :explanation 83 validates :facility_id 84 validates :scale_id 85 validates :category_id 86 end 87 with_options numericality: { other_than: 1 } do 88 validates :facility_id 89 validates :scale_id 90 validates :category_id 91 end 92 93 94 def save 95 event = Event.create(name: name, explanation: explanation, facility_id: facility_id, scale_id: scale_id, category_id: category_id, volunteer: volunteer,user_id: user_id) 96 tag = Tag.where(tagname: tagname).first_or_initialize 97 tag.save 98 99 EventTagRelation.create(event_id: event.id, tag_id: tag.id) 100 end 101 102 end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/27 04:46
2020/11/27 04:51
2020/11/27 05:43
2020/11/27 09:14
2020/11/27 09:21
2020/11/27 10:38
2020/11/27 10:45