How can a rails form be filtered to show only obje

2019-09-18 04:47发布

问题:

I'm using acts_as_taggable_on and tags are associated to a Brand model. Users, via a User model, then add tags to each brand via a form partial. The problem is that the form partial (in an update method in the controller), shows all the tags specific to the brand instance, instead of showing only the ones that are tagged by the current user's tagger_id.

The second part to this is that I want the form to only show brand instances that have no tags by the current user yet. I have defined the empty tags in the brand model below but don't know how to call them up in the form fields.

Form View

<%= form_for @brand, :html => {:multipart => true} do |f| %>  
<%= f.label :tag_list, "Your tags" %>  <%= f.text_field :tag_list %>
<%= f.submit "Tag" %></p>

Controller

@brand = current_user.brands.includes(:taggings).where(:taggings => { :id => nil } ).order("RANDOM()").first

回答1:

The first part to the question is solved by added the following to the form:

<%= f.label :tag_list, "Your tags" %>  <%= f.text_field :tag_list, :value => @brand.tags_from(current_user) %>