How do I update & display a current user's tags, tagged with acts_as_taggable_on?

巧了我就是萌 提交于 2020-01-06 17:15:30

问题


I'm not sure how to display tags in my view that belong to a user logged in with Omniauth.

A page in the view loads a random photos and tags associated to it (via a form that can be updated from that page). It works, but when I log in with Facebook account A, it will show exactly the same tags as if I log in with Facebook account B.

Getting the whiny nil error with this below Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

The view is:

<%= render 'tag_form' %>

Updated: The form is:

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

# <%= f.text_field :tag_list %> was changed to the one above.

The tags are also called in a user's dashboard, as below (currently empty because obviously I can't update tags right now):

<%= brand.taggings( :tagger_id => current_user.id, :tagger_type => 'User').collect{|tagging| tagging.tag.to_s}.join(", ") %>

The application controller showing current user

def current_user  
  @current_user ||= User.find(session[:user_id]) if session[:user_id]  
end

The Brand controller

helper_method :current_user 

def index
  @brands = Brand.all
  @brand = Brand.order("RANDOM()").first
end

Added: The Brand model

attr_accessible :name, :tag_list, :current_user
acts_as_taggable_on :tags

belongs_to :user

before_save :set_tag_owner

def set_tag_owner
  set_owner_tag_list_on(@brand, :tags, self.tag_list)
  self.tag_list = nil
end

回答1:


Was just talking to you on IRC, but can you try:

<%= debug @brand.tag_list %>

in your view. Your user doesn't have tags. If you want tags for your user add a acts_as_taggable in your User model


It seems you want to find tags of @brand tagged by current_user. In that case:

<%= debug @brand.taggings(:tagger_id => current_user.id, :tagger_type => 'User').all %> 



回答2:


I removed this from my model:

before_save :set_tag_owner

def set_tag_owner
    set_owner_tag_list_on(@clown, :tags, self.tag_list)
    self.tag_list = nil
end

I added this to the controller in an action that handles PUT requests instead of GET requests (Update method)

current_user.tag(@clown, :with => params[:brand][:tag_list], :on => :tags)

It works now.



来源:https://stackoverflow.com/questions/6933659/how-do-i-update-display-a-current-users-tags-tagged-with-acts-as-taggable-on

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