Rails 5 - NoMethod error - undefined method

In the application I'm going to learn Rails, I have a polymorphic relationship between the "TAG" model and the "ANNOTATION" and "DOCUMENT" models. Like the model articles and comments.

Creating and destroying tags works. Now I want to update the tags and run a method error that I don't understand. I tried many alternatives (for example, using <%= link_to tag.content, [object, tag]...with the form <%= simple_form_for object.tag ...).

The form is called using:

<% object.tags.each do |tag| %>
        <% unless tag.content.blank? %>
        <tr>
            <td><%= link_to tag.content, @tag, method: :patch %></td>

This is the tag controller:

class TagsController < ApplicationController

def index

end

def create
  tagable = detect_tagable
  tagable.tags.create(tag_params)
  redirect_to tagable_path(tagable)
end

def update
  tagable = detect_tagable
  @tag = tagable.tags.find(params[:id]) 
  @tag.save
  render '_tag_update'
end

def destroy
  tagable = detect_tagable
  @tag = tagable.tags.find(params[:id])
  @tag.destroy
  redirect_to tagable_path(tagable)
end

private

  def tagable_path(tagable)
    case tagable
    when Document
      document_path(tagable)
    when Annotation
      annotate_path(tagable)
    else
      fail 'Unknown tagable'
    end
  end

  def detect_tagable
    if params[:annotation_id]
      Annotation.find(params[:annotation_id])
    elsif params[:document_id]
      Document.find(params[:document_id])
    else
      fail 'Tagable not found'
    end
  end

  def tag_params
    params.require(:tag).permit(:content, :location, :tagtype_id,annotation_attributes: { annotation_ids:[] }, document_attributes: { document_ids:[] })
  end

end

It displays the correct form _tag_update.html.erbwith the correct parameters (annotation identifier and tag identifier), but causes an error:

<%= simple_form_for @tag, html: { class: 'form-vertical', multipart: true },

Complete mistake

NoMethodError # update /Users/Dimitri/Documents/AppDev/shine/app/views/tags/ _tag_update.html.erb, # 1 : undefined `tag_path ' # < #: 0x007fc2aede9d88 > ? tagtype_path ( # 1): 1 2 3 4 5 6
<% = simple_form_for @tag, html: {class: 'form-vertical', multipart: true},     wrapper:: horizontal_form,     wrapper_mappings: {     check_boxes:: horizontal_radio_and_checkboxes,     radio_buttons:: horizontal_radio_and_checkboxes,     :: horizontal_file_input, Rails.root://Dimitri/Documents/AppDev/shine

| | :

app/views/tags/_tag_update.html.erb: 1: in _app_views_tags__tag_update_html_erb___1949489846228898836_70237067101380' app/controllers/tags_controller.rb:17:in update '

:

{"_method"=>"patch", "authenticity_token"=>"LhqKjyjbYdznMvx+GjsIL0phwT8pRTtanooKU6Xt4hHaPRFMmZJmZVm7GGZa8iaWxN1MIfm7xHwwhSSrSBoO/g==", "annotation_id"=>"6", "id"=>"24"}

0
1

link_to, form_for redirect_to, ( ).

, , :

simple_form_for( [@tag.taggable, @tag],  # ...

link_to( @tag.content, [@tag.taggable, @tag] )

redirect_to( [@tag.taggable, @tag] )

:

def tagable_path(tagable)
  case tagable
  when Document
    document_path(tagable)
  when Annotation
    annotate_path(tagable)
  else
    fail 'Unknown tagable'
  end
end

redirect_to taggable , .

.

. , :

# avoids duplication
concern :taggable do
  resources :tags, only: [:new, :index, :create]
end

# generates GET|PATCH|DELETE /tags/:id and /tags/:id/edit
resources :tags, only: [:show, :edit, :destroy, :update]

resources :documents, concerns: :taggable
resources :annotations, concerns: :taggable

resources :annotations, shallow: true .

, redirect_to(@tag) link_to('Delete tag', @tag, method: :delete )

+1

Source: https://habr.com/ru/post/1658018/


All Articles