I have 2 associated models (items and cities - cities is already populated by me) and a form where users can enter things they notice in a city. They can now enter the name of that thing and the ID of the city. However, obviously I would like to let them enter the NAME of the city and get the id of that name, given that his city exists in my database.
What can I do to achieve that and get the id?
Here are my models:
class City < ActiveRecord::Base
has_many :items
end
class Item < ActiveRecord::Base
belongs_to :city
end
and the reference in schema.rb
add_index "items", ["city_id"], name: "index_items_on_city_id", using: :btree
Form:
<%= form_for :item, url: items_path do |f| %>
<%= f.text_field :name %>
<%= f.text_field :city_id %>
<% end %>
Items controller:
def create
@item = Item.new(params[:item].permit(:name, :city_id))
end
Thanks a lot!