Why my textbox shows ActiveRecord_Associations_Col

2019-08-06 02:15发布

问题:

I'm building rails application and I use form_for to render the form. Everything works fine works fine except one text field has ActiveRecord_Associations_CollectionProxy inside the text which I'm not sure where it's coming from.

Here's my form

<%= form_for(@video, html: { class: "directUpload" }, multipart: true) do |f| %>
  <% if @video.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@video.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @video.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :path %><br>
    <%= f.file_field :path%>
  </div>
  <div class="field">
    <%= f.label :tags %><br>
    <%= f.text_field :tags %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And here's my Video model

class Video < ActiveRecord::Base
  belongs_to :user

  has_many :video_tags
  has_many :tags, through: :video_tags
end

Here's the screenshot

回答1:

You have multiple tags that you want show in form. Your controller could serve all tags for form where you could show them with http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select for example.