Unknown19 Unknown19 - 6 months ago 3
Ruby Question

Rails - How to call values from one model into another model form

I'm working on some app and I have 2 models. Categories in which I create category names and Question. Category has_many questions and Question belongs_to Category.

I've added category_id to Question model.

Now I need to take all Category_names and display them in form where I create Question so User can choose in which category_name will save question.

I've tried something like this in firs line of code but not working.

<%= f.input :category_id, Category.all.map(&:name) %>
<%= f.input :question_name, wrapper: :vertical_text_input, as: :text %>
<%= link_to "Markdown help", "http://assemble.io/docs/Cheatsheet-Markdown.html", target: "_blank", class: "right" %>
<%= f.input :answer %>
<%= f.input :image, as: :attachinary %>

QUESTION: How to display all Category names in form where I create new Questions?


you may wanna use a select box and use the collection select rails helper :

f.collection_select(:category_id, Category.all, :id, :name)