-1

I have a question about acts_as_taggable_on. I'm making a BBS and want to display link tags. However, my website can display only strings not links. So I want to put link to strings.

I want to link_to each tags#show.

Now some tags are saved in Tag table and this table have these column(id, name, created_count)

This is my code:

post.rb

def save_tags
 array = self.check_taggable_word(self.title)
 self.tag_list.add(array, parse: true)
end
def tag_lists
 tag_lists = self.tag_list
end
def check_taggable_word(text)
 ary = Array.new
 nm = Natto::MeCab.new
 nm.parse(text) do |n|
   ary<<n.surface
 end
 tags = ActsAsTaggableOn::Tag.pluck(:name)
 return ary & tags
end

show.html.erb (post)

  Tag:
  <% @post.save_tags %>
  <%= @post.tag_list %>

posts_controller.rb

def show
    @post = Post.find(params[:id])
    @category = @post.category
end

routes.rb

Rails.application.routes.draw do
 root 'static_pages#home'
 get '/about' => 'static_pages#about'
 get '/contact' => 'static_pages#contact'
 resources :categories do
  resources :posts
 end
 resources :posts do
  resources :comments
 end
 resources :tags, only: [:index, :show]
end
tokyo.a
  • 11
  • 2

1 Answers1

1

It is not so sure what you are trying to achieve, and it might help if you post your controller , too.

However, displaying links can be achieved in rails by using the link_to helper

<% = link_to 'text', some_path %>

Your target is to connect the link to your controller action where you handle the 'tags' The gem you mentioned gives you another example of displaying links in views. This might work for your depending on your naming conventions in your controller.

<% tag_cloud(@tags) do |tag| %>
  <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => 'css_class' %>
<% end %>

In general: you need to call

@tags = Tags.all 

somewhere in your controllers to get all entries for the tags. Than you can loop over tags which each...that can produce you a link of all your tags and if you want to link to tags#show the link must go to an action where you find that specific tag. For example this can be in that action:

@tag = Tag.find(params[:id])

. That's it.

OK i edit once again. TRY:

in you post controller#show you can show all tags for that post like so

@post.tags.each do |tag|
<%= link_to tag.name, tag %>

this requires that your tag has attribute "name"

in your post#index you can do

<%@post.each do |post|%>
<%post.tags.each do |tag|%>
<%= link_to tag.name, tag %><%end%><%end%>

if you just want all tags to be displayed without post etc. TRY:

in your controller posts#index

@tags = Tag.all 

<%@tags.each do |tag|%>
<%= link_to tag.name, tag %><%end%>

In your current controller action posts#show you will only find the tags for that @post because all you do is finding the post by id. So you will only find tags for that post if the association is correct.

That allows you to do @post.tags.each do |tag|...etc. tag than can be used for the link_to helper.
If you use <%= link_to tag.name, tag %> it will show you a link with the tag.name that links to that specific tag for each tag that belongs to the post.

KcUS_unico
  • 513
  • 3
  • 9
  • Thank you so much KcUS_unico. i add two tags and then tried above code but `wrong number of arguments (1 for 2)` error displayed. – tokyo.a Mar 01 '17 at 09:11
  • try: <% @post.tag_list.each do |tag| %> <%= link_to tag, tag %> <%end%> – KcUS_unico Mar 01 '17 at 09:23
  • this code looks work. but this link path to (.../posts/"tag.name"). i want to path to (.../tags/"tag.name"). Thank you so much KcUS_unico. – tokyo.a Mar 01 '17 at 09:26
  • Ya, you need than to have a route for this action in your routes.rb. Do you have that? – KcUS_unico Mar 01 '17 at 09:29
  • thank you KcUS! i just update my **routes.rb** to above. – tokyo.a Mar 01 '17 at 09:33
  • Well, what you need is basically an 'index' and or 'show' action for all you tags. So when you want to link to all tags that exist you can try: <%= @tags.map{|tag| link_to(tag.name,tag )}.join(' , ') %> for this you need @tags = Tag.all in your controller action posts#show or whereever you put this – KcUS_unico Mar 01 '17 at 09:34
  • GREAT! within tags#index you can <% tags.each do |tag| %> <%= link_to tag.name, tag %><%end%> which will lead you to the #show action of tags...but for this to work you also need a tags_controller and the two templates..... – KcUS_unico Mar 01 '17 at 09:36
  • In general: you need to call @tags = Tags.all somewhere in your controllers. Than you can loop over tags which each...that can produce you a link of all your tags and if you want to link to tags#show the link must go to an action where you find that specific tag for instance tag = Tag.find(params[:id]). That's it. – KcUS_unico Mar 01 '17 at 09:39
  • finally how can i display link_to in show(post). i can't understand what should i write in routes.rb after you said ** you need than to have a route for this action in your routes.rb.** – tokyo.a Mar 01 '17 at 09:49
  • Well, I edited my answer a bit. To display all tags in your post#show view you need to initialize all tags in that controller action. So add this line : @tags = Tag.all for the each loop to work – KcUS_unico Mar 01 '17 at 09:56
  • i gotcha. sorry my silly question. i actually wanna add tags by using mecab which separate post to each words. and then if some words match with tags in tag table. then return those or that words to show(post). and it is linked_to each tag#show – tokyo.a Mar 01 '17 at 10:09
  • so i don't want to display all tags in tag table. Just only tags which is related to post. – tokyo.a Mar 01 '17 at 10:10
  • hi KcUS_unico. I still try to fix it. however this error show `wrong number of arguments (1 for 2)`. now i rewrite `@tags = Tags.all ` this code to `@tags = ActsAsTaggableOn::Tag.all`, because i use acts_as_taggable_on, so i cant designate like what you said.... thank you – tokyo.a Mar 02 '17 at 04:46
  • Hey, I am not sure. I think you know the principe. You need the instances for TAGS and than you can make an each loop and create a link for every TAG. I unfortunately, don't have an exact answer for you now. Would like to help...but don't know exact answer. I think you can manage though. – KcUS_unico Mar 02 '17 at 07:31
  • hi KcUS_unico.I could display tag_links in post/show.html.erb. thank you. and then now i have new question, could you answer my question>?? http://stackoverflow.com/questions/42745034/rails-display-post-tag-links-in-tags-show-html-erb-acts-as-taggable-on – tokyo.a Mar 12 '17 at 07:58