Nenzo
Nenzo

Reputation: 145

Insert value of select_box into table attribute

I've got two tables:

Topics
-name 

and

Queries
-topic_id

A query can have a topic and so I'm trying to create a select_box in my queries_form which inserts a selected topic into my topic_id attribute of my queries table.

What I already made is a functional select_box, but I'm unable to insert the selected item into the topic_id attribute...

<% form_for @query do |f| %>
....
<%= f.select :topic_id, :value => 'queries', Topic::find(:all).collect( &:name )  %>
<% f.submit "save" %>
<% end %>

Thanks a lot for helping me

Upvotes: 0

Views: 135

Answers (1)

SteenhouwerD
SteenhouwerD

Reputation: 1807

First set your relationships in your model like this :

class Topic < ActiveRecord::Base
  has_many :queries
end

and

class Query < ActiveRecord::Base
  belongs_to :topic
end

Then you can write in your form view this:

<% form_for @query do |f| %>
  ....
  <%= f.select :topic_id, Topic.all.collect {|topic| [topic.name, topic.id]}  %>
  <% f.submit "save" %>
<% end %>

Upvotes: 1

Related Questions