轨道选择下拉列表,控制器中的内容不起作用



我有一个模型 Tag 中的项目列表,我想在下拉字段中显示它。用户将选择一个,它将被添加到聊天对象中。与 Chat::Tags 存在 1:多关系,存储在标记表中。

So--用户从下拉列表中选择一个标签并单击"添加标签",聊天页面将刷新,新标签将添加到聊天页面(并作为聊天和标签的外键存储在标签表中)。

这是我所拥有的...

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

在节目中.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>",

--编辑--

标记表为:

["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

这不起作用的原因是因为表单用于@chat,而聊天没有名为 tag_id 的方法。在窗体中调用它的方式是使用 f 对象。如果您想更改/更新该表单中的标记...

从这里更改您的collection_select

= f.collection_select(:tag_id, @tags, :id, :name, include_blank: true)

对此

= collection_select(:taggings, :tag_id, @tags, :id, :name, include_blank: true)

然后在您的控制器中更改此内容

tagging = @chat.taggings.create(tag_id: params[:tag_id], coordinator: current_coordinator)

对此

tagging = @chat.taggings.create(tag_id: params[:taggings][:tag_id], coordinator: current_coordinator)

最新更新