在我的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
我的错误/错误在哪里?
答案 0 :(得分:1)
你能告诉我们你的控制器吗?可能这是object
中的拼写错误,@object
真的会this comment说
无论如何,请发送您的控制器代码以确认此
在您的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