0

I have a question model in my rails app. A user can add questions. When adding a question a user can assign it topics and a place.

I followed the railscast on how to set up acts_as_taggable_on for tags, in my case topics.

However, I now want to add the place feature. I had a go myself and had a google around but couldn't find much on having 2 acts_as_taggable items.

If someone could advise me on how to setup a second acts_as_taggable feature on the same acts_as_taggable that would be great

My code so far:

Question.rb
acts_as_taggable   
acts_as_taggable_on :topics, :places



questions_controller.rb
def topics
if params[:topic].present? 
  @questions = Question.tagged_with(params[:topic])
else 
  @questions = Question.postall
end  
end



questions/show.html.erb
<p>Topics: <%= raw @question.tag_list.map { |t| link_to t, topic_path(t) }.join(', ') %></p>


form.html.erb
<%= f.input :tag_list, placeholder: "Topics" %>
jmromer
  • 2,212
  • 1
  • 12
  • 24
Lewis Frost
  • 557
  • 1
  • 5
  • 25

1 Answers1

0

you have done it right in model..

just add some code to controller and view

def topics
if params[:topic].present? 
  @questions = Question.tagged_with([params[:topic]], :on => :topics, :any => true)
elsif params[:place].present?
  @questions = Question.tagged_with([params[:place]], :on => :places, :any => true)
else
  @questions = Question.postall
end  
Anil Maurya
  • 2,298
  • 1
  • 22
  • 28
  • Thanks for your help Anil, I thought this was the case. I have a quick question for you though. I am not sure what to put in the form. Currently for my topics in the form i am using<%= f.input :tag_list, placeholder: "Topics" %>. So what would I put for the place field? Thanks – Lewis Frost Jan 07 '14 at 10:50
  • <%= f.input :tag_list, placeholder: 'Topic' %> this will insert in tag_list which is wrong, you have to insert in topic_list so , use <%= f.input :topic_list, placeholder: 'Topic' %> instead, and for place field use <%= f.input :place_list, placeholder: 'Place' %> – Anil Maurya Jan 07 '14 at 12:33
  • Hi Anil, yes I realised this after I posted the comment! One more thing...Its now all working perfectly. However, a place can be two words, e.g New York. In the url, it is showing /places/New%20York. I've tried friendly_id as i'm using this for the quesitons' slug but i've had no luck so far. Any ideas? – Lewis Frost Jan 07 '14 at 12:36
  • friendly_id must solve your problem. You must be using it wrong. In your model add friendly_id :places, use: :slugged – Anil Maurya Jan 07 '14 at 13:37
  • Hi Anil, thanks again for your help. The problem is that friendly_id is already being used for the question friendly_id :title, use: [:slugged, :finders]. So when i add the line friendly_id :places, use: :slugged. And try to add a question i get an error. I'm sure i'm just not using it properly. But i can't see how to use it since the 'places' does not have a model. Any ideas? – Lewis Frost Jan 07 '14 at 15:47