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

2

0

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
Outstand answered 3/8, 2011 at 21:40 Comment(0)
A
0

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 %> 
Acculturation answered 9/8, 2011 at 7:9 Comment(6)
Rene, your help has been invaluable. My last issue is that the condition tagger_id => current.user.id doesn't work. Currently all saved tags still show. Server shows ((taggings.tagger_id IS NULL AND taggings.context = 'tags'))) which is probably why.Outstand
Your code above needs a .where after taggings. I added that in and now it does the right thing where it doesn't show tags that aren't a current user's but now there are no tags for the same reason as my above comment (tagger_id is NULL) so it has no tags to show.Outstand
Ah, I suspect it has to do with the form partial. The form text field loads tags that were previously added, but it loads all tags and not just for current_user. So when it saves, it's not saving to current_user.id but to NULL. How to fix this?Outstand
Ah yeah... it needed the .where there. If it shows you a tagger_id IS NULL this means we can't access current_user there. If this is in a partial and you're able to access the current_user before in the partial add a , :locals => { :current_user => current_user } to the render :partial callAcculturation
Unfortunately that didn't help, even added an attr_accessible for :current_user but that didn't work either.Outstand
Did you also set the current_user?Acculturation
O
0

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.

Outstand answered 10/8, 2011 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.