Home>

・ I want to solve
I'm making an event posting app, but when I try to post a new one, I get an error trying to move to the tag path.

・ Error statement

NoMethodError in Events # create
Showing /Users/user/projects/asomemo/app/views/events/_form.html.erb where line # 1 raised:
undefined method `events_tags_path'for #<#<Class: 0x00007fbbf5c2b038>: 0x00007fbbf5c29738>
Did you mean? events_path
Extracted source (around line # 1):
<% = form_with (model: @event, local: true) do | f |%>
<% = render'shared/error_messages', model: f.object%>
  
  <label>event name</label>
  <% = f.text_field: name, class :: form_control%>

new.html

Code 
  
    
    <span>
        New post
    </span>
        <% = render partial: "form", locals: {event: @event}%>
      <% # Display form with partial template%>
    
  


form.html (corresponding code)

<% = form_with (model: @event, local: true) do | f |%>
<% = render'shared/error_messages', model: f.object%>
  
route

route

Rails.application.routes.draw do
  devise_for: users
  root to: "events # index"
  resources: events, only: [: index,: new,: create,: show,: destroy,: edit,: update] do
    collection do
      get'search'
    end
  end
end


events_controller

`` `ruby
class EventsController</pre>
<p>_event_model</p>
<pre><code data-language = "Ruby">class Event</pre>
<p>event_tag.model</p>
<pre><code data-language = "Ruby">class EventsTag
include ActiveModel :: Model
attr_accessor: name,: expansion,: facility_id,: scale_id,: category_id,: volunteer,: tagname,: user_id

with_options presence: true do
validates: name
validates: expansion
validates: facility_id
validates: scale_id
validates: category_id
end
with_options numericality: {other_than: 1} do
validates: facility_id
validates: scale_id
validates: category_id
end

def save
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)
tag = Tag.where (tagname: tagname) .first_or_initialize
tag.save
EventTagRelation.create (event_id: event.id, tag_id: tag.id)
end
end

`` ```

  • Answer # 1

    @event = EventsTag.new (event_params)

    EventsTagWhat is it?EventIs it different from?
    And whyEventsController # newOr#createsoEventsTagWill you create a new one?


    Postscript
    model_nameSeems to be the best to hack

    class EventsTag
      def self.model_name
        @_model_name || = Event.model_name
      end
    end

    Another thing to worry about
    Instance variables when calling a partial template@eventThe local variableeventI'm passing it as
    Instance variables in the partial template@eventIs used.

    <% = form_with model: event, local: true do | f |%>

    It should be okay.