rails4有很多:如何在new.html.erb中获取值

6tdlim6h  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(312)

我使用rails4.2.8,ruby2.5.0。
我的关系是多层次的 questions , classifies , tags ,表关系如下:

我的模型:

  1. class Question < ActiveRecord::Base
  2. has_many :taggables,:dependent => :destroy, inverse_of: :question
  3. has_many :tags, through: :taggables, :dependent => :destroy
  4. belongs_to :classify
  5. belongs_to :user
  6. accepts_nested_attributes_for :tags
  7. accepts_nested_attributes_for :taggables
  8. end
  9. class Tag < ActiveRecord::Base
  10. has_many :taggables, :dependent => :destroy,inverse_of: :question
  11. has_many :questions, through: :taggables, :dependent => :destroy,inverse_of: :question
  12. belongs_to :classify
  13. end
  14. class Classify < ActiveRecord::Base
  15. has_many :questions, :dependent => :destroy, inverse_of: :classify
  16. has_many :tags, :dependent => :destroy, inverse_of: :classify
  17. accepts_nested_attributes_for :questions
  18. accepts_nested_attributes_for :tags
  19. end
  20. class Taggable < ActiveRecord::Base
  21. belongs_to :question
  22. belongs_to :tag
  23. accepts_nested_attributes_for :question
  24. end

我的数据库:

  1. class CreateQuestions < ActiveRecord::Migration
  2. def change
  3. create_table :questions do |t|
  4. t.string :title
  5. t.text :content
  6. t.integer :classify_id
  7. t.timestamps
  8. end
  9. end
  10. end
  11. class CreateTags < ActiveRecord::Migration
  12. def change
  13. create_table :tags do |t|
  14. t.string :name
  15. t.text :content
  16. t.integer :classify_id
  17. t.timestamps
  18. end
  19. end
  20. end
  21. class CreateClassifies < ActiveRecord::Migration
  22. def change
  23. create_table :classifies do |t|
  24. t.string :name
  25. t.text :content
  26. t.timestamps
  27. end
  28. end
  29. end
  30. class CreateTaggables < ActiveRecord::Migration
  31. def change
  32. create_table :taggables do |t|
  33. t.integer :question_id
  34. t.integer :tag_id
  35. t.timestamps
  36. end
  37. end
  38. end

我的控制器:

  1. questions_controller.rb
  2. def new
  3. @question = Question.new
  4. @question.taggables.build
  5. @question.tags.build
  6. end
  7. classifies_controller.rb
  8. def new
  9. @classify = Classify.new
  10. @classify.questions.build
  11. @classify.tags.build
  12. end
  13. tags_controller.rb
  14. def new
  15. @tag = Tag.new
  16. @tag.taggables.build
  17. @tag.questions.build
  18. end

我的 questions/new.html.erb ```
<%= form_for(@question) do |f| %>

<%= f.label :classify, "Classify and Tags" %>


<%= f.select :classify_id, Classify.all.collect { |c| [c.name, c.id]},{}, class: "form-control" %> ##I can get the classify_id in questions


<%= f.text_field :tag, class: "",multiple:"multiple",placeholder: "Please input the tags" %>

## There how can I get the input tags , and record the id and name in tags table , taggables table? Then how can I show the tags name in the question show.html.erb?

  1. </div>
  2. </div>
  3. </div>
  4. <% end %>
  1. 我想要的是如何记录输入标签idname `tags table` , `taggables table` ,那么如何显示问题中的标签名称 `show.html.erb` ?
  2. 谢谢你的帮助。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题