Rails -在分配属性时,必须传递一个哈希值作为参数2



在我的Rails 5应用程序中,我有这个错误:

当我使用此表列出相关对象(在本例中是Annotation)的标记时

<tbody>
        <% object.tags.each do |tag| %>
            <% unless tag.content.blank? %>
            <tr>
                <td style="word-wrap: break-word;" class="displaytagedit"><%= link_to tag.content, **[object, tag]**, method: :patch %></td>

它尝试打开这个链接

http://localhost:3000/annotations/6/tags/24(看起来正确)

并抛出这个错误:

在分配属性时,必须传递一个哈希值作为参数。

在我的控制器的这一部分(下面)

tagable = detect_tagable
@tag = tagable.tags.update(params[:id])
@tags = Tag.all
render '_tag_update'

结束

它应该调用这个表单:

<%= 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,
    boolean: :horizontal_boolean
    } do |f| %>
    <%= f.error_notification %>
    <%= f.input :content, placeholder: 'Tagged content', label: false %>
    <%= f.association :tagtype, prompt: 'Select tag type', label: false, :collection => Tagtype.active.order(:name).where(:documenttype => object.documenttype_id) %>
    <%= f.input :location, :as => :hidden, :input_html => { :value => 'x=0, y=0' }, label: false %>
    <%= f.button :submit %>
<% end -%>

标签是2个对象(目前)上的可重用模型。

这是routes.rb

Rails.application.routes.draw do
  root 'dashboard#index'
  devise_for :users
  resources :users, :documenttypes, :tagtypes, :business_partners
  resources :documents do
    resources :comments, :tags
    get "pdf", on: :member 
  end
  resources :annotations do
    resources :comments, :tags
    get "pdf", on: :member
end

更新

这是标签控制器:

class TagsController < ApplicationController
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])
   @tags = Tag.all
   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

我的错误在哪里?

能给我们展示一下你的控制器吗?可能是object打错了实际上应该是@object

无论如何,发送你的控制器代码来确认这个

编辑:

在你的TagsController文件中,你必须这样设置更新方法:

def update
  tagable = detect_tagable
  @tag = tagable.tags.find(params[:id])
  @tags = Tag.all #Or whatever query you want if you want to select more specific Tags
  render '_tag_update'
end

最新更新