Rails 5 - NoMethod 错误 - 未定义的方法

Rails 5 - NoMethod Error - undefined method

在我为学习 Rails 而构建的应用程序中,模型 "TAG" 与模型 "ANNOTATION" 和 "DOCUMENT" 之间存在多态关系。类似于文章和评论模型。

正在创建和销毁标签。现在,我想将标签和 运行 更新为我不理解的无方法错误。尝试了许多替代方案(例如,使用 <%= link_to tag.content, [object, tag]...<%= simple_form_for object.tag ... 形式)。

使用以下方式调用表单:

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

这是标签控制器:

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

它使用正确的参数(注释 ID 和标记 ID)呈现正确的形式 _tag_update.html.erb,但在以下位置抛出错误:

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

完全错误

NoMethodError in Tags#update Showing /Users/Dimitri/Documents/AppDev/shine/app/views/tags/_tag_update.html.erb where line #1 raised: undefined method `tag_path' for #<#:0x007fc2aede9d88> Did you mean? tagtype_path Extracted source (around line #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, file: :horizontal_file_input, Rails.root: /Users/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:inupdate' Request

参数:

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

当您将记录传递给 link_to 时,form_forredirect_to rails 将记录传递给 polymorphic route helpers(请注意,这与处理多态关联)。

要生成到嵌套资源的路由,您需要同时传递父记录和子记录:

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 和 rails 将使用巧妙的约定为您找出路线。

嵌套并不总是好的。

成员路由不需要嵌套。由于每条记录都可以通过唯一的 ID 访问,因此您可以取消嵌套成员路由:

# 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 )