How can a rails form be filtered to show only objects without tags, and by a current user?

喜欢而已 提交于 2019-12-12 00:53:33

问题


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) %>


来源:https://stackoverflow.com/questions/7042781/how-can-a-rails-form-be-filtered-to-show-only-objects-without-tags-and-by-a-cur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!