添加标签以在轨道中查看并在路线中按标签显示资源?

时间:2017-05-26 11:07:23

标签: html ruby-on-rails ruby

我试图在我的QuestionsController / Question模型中添加标记系统。基本上,我想用一个或多个标签标记一个问题(例如,生物学,解剖学)

我的问题如下:

1。如何让我的视图显示new.html.erb表单中提交/选定的标记?

我在视图中尝试<% @question.tags %>,但不会返回错误,但 Tag::ActiveRecord_Associations_CollectionProxy:0x007fda43be4900>

2。如何在新视图中显示所有标记有相关ID的问题?例如,如果标签名为anatomy,tag_id为1,我将如何在新视图中显示该标签的所有问题?

这是我的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

这是我的问题模型

class Question < ActiveRecord::Base

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

这是我的新问题视图

    <%= 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 %>

这是我的节目问题视图

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

这是我的tagging.rb模型

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

这是我的tag.rb模型

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

1 个答案:

答案 0 :(得分:2)

<强> 1。如何让我的视图显示new.html.erb表单中提交/选定的标记?

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

<强> 2。如何在新视图中显示所有标记有相关ID的问题?例如,如果标签名为anatomy,tag_id为1,我将如何在新视图中显示该标签的所有问题?

我会尝试给出最简单的答案。 将索引方法更改为:

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

现在尝试调用网址

  

/问题?TAG_ID = 1