我有一個模型中的項目列表,標籤,我想在下拉字段中顯示。用戶將選擇一個,並將其添加到聊天對象。與聊天::標籤存在1:多關係,存儲在標籤表中。導軌選擇下拉控制器不工作的內容
所以 - 用戶從下拉列表中選擇一個標籤並點擊「添加標籤」,聊天頁面被刷新,新的標籤添加到聊天頁面(並作爲外鍵存儲在標籤表中聊天和標記)。
這裏就是我有...
chats_controller.rb:
def show
@chat = Chat.find params[:id]
@tags = Tag.order(:name)
end
def update
@chat = Chat.find params[:id]
tagging = @chat.taggings.create(tag_id: params[:tag_id], coordinator: current_coordinator)
flash[:success] if tagging.present?
end
而且在show.html.haml:
.li
= form_for @chat, url: logs_chat_path(@chat), method: :put do |f|
= f.collection_select(:tag_id, @tags, :id, :name, include_blank: true)
= f.submit "Add Tag"
現在,它返回以下錯誤:
"exception": "NoMethodError : undefined method `tag_id' for #<Chat:0x000000073f04b0>",
--edit -
該表的Tagging是:
["id", "chat_id", "tag_id", "coordinator_id", "created_at", "updated_at"]
耙路線顯示:
logs_chats GET /logs/chats(.:format) logs/chats#index
POST /logs/chats(.:format) logs/chats#create
new_logs_chat GET /logs/chats/new(.:format) logs/chats#new
edit_logs_chat GET /logs/chats/:id/edit(.:format) logs/chats#edit
logs_chat GET /logs/chats/:id(.:format) logs/chats#show
PATCH /logs/chats/:id(.:format) logs/chats#update
PUT /logs/chats/:id(.:format) logs/chats#update
DELETE /logs/chats/:id(.:format) logs/chats#destroy
不應該是id代替tag_id像這樣:@ chat.taggings.create(id:params [:tag_id],coordinator:current_coordinator) –
沒有工作......這是有道理的,因爲它與表格列不匹配。這意味着我希望標記表的ID爲tag_id,但實際上(如您在更新的表列中看到的)我想要tag_id = tag_id。 ID本身應該是自動生成的。 –