mazing
mazing

Reputation: 743

Adding tags to view in rails and showing resource by tag in route?

I'm trying to add a tagging system to my QuestionsController / Question model. Basically, I want to tag a question with one or more tags (eg; biology, anatomy)

My questions are as follows:

1. How can I get my view to show the submitted/selected tags from the new.html.erb form?

I've tried <% @question.tags %> in the view but that returns not an error, but this Tag::ActiveRecord_Associations_CollectionProxy:0x007fda43be4900>

2. How can I show all questions tagged with the relevant id in a new view? For example, if a tag is called anatomy with a tag_id of 1, how would I go about showing all the questions with that tag in a new view?

Here is my questions_controller.rb

class QuestionsController < ApplicationController
    def new
        @question = Question.new
    end
    def index
        @questions = Question.all
    end

    def show
        @question = Question.find(params[:id])  
    end
    def create
        @question = Question.new(question_params)
        # @question.save returns a boolean indicating whether the article was saved or not.
        if @question.save 
            redirect_to @question, notice: 'Created Question!'
        else
            render 'new'
        end
    end

    private
        def question_params
            params.require(:question).permit(:title, 
                                             :text, 
                                             :answer1,
                                             :answer2,
                                             :answer3,
                                             :answer4,
                                             :answer5,
                                             { :tag_ids => [] })
        end

end

Here is my question model

class Question < ActiveRecord::Base

  has_many :taggings
  has_many :tags, through: :taggings
...

Here is my new questions view

    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>

    <%= f.label :answer1, "Answer Choice 1" %><br>
    <%= f.text_field :answer1 %><br>

    <%= f.label :answer2, "Answer Choice 2" %><br>
    <%= f.text_field :answer2 %><br>

    <%= f.label :answer3, "Answer Choice 3" %><br>
    <%= f.text_field :answer3 %><br>

    <%= f.label :answer4, "Answer Choice 4" %><br>
    <%= f.text_field :answer4 %><br>

    <%= f.label :answer5, "Answer Choice 5" %><br>
    <%= f.text_field :answer5 %><br>

  <%= f.label :tags %><br>
    <%= collection_check_boxes(:question, :tag_ids, Tag.all, :id, :name) %>
  <p>
    <%= f.submit %>

Here is my show question view

...
<p>
<strong>tags</strong><br>
<%= @question.tags %>
</p>

Here is my tagging.rb model

class Tagging < ActiveRecord::Base
  belongs_to :question  # foreign key - post_id
  belongs_to :tag   # foreign key - tag_id
end

Here is my tag.rb model

class Tag < ActiveRecord::Base
    has_many :taggings
    has_many :questions, through: :taggings # notice the use of the plural model name
end

Upvotes: 1

Views: 430

Answers (1)

Sajin
Sajin

Reputation: 1638

1. How can I get my view to show the submitted/selected tags from the new.html.erb form?

<%= @question.tags.map{|t| t.name}.join(",") %>

2. How can I show all questions tagged with the relevant id in a new view? For example, if a tag is called anatomy with a tag_id of 1, how would I go about showing all the questions with that tag in a new view?

I'll try to give the simplest answer. Change your index method to :

def index
    if params[:tag_id].present?
        tag = Tag.find(params[:tag_id])
        @questions = tag.questions
    else
        @questions = Question.all
    end
end

Now try calling the url

/questions?tag_id=1

Upvotes: 2

Related Questions