0

I am trying to create a form where the user gives the title of an album and picks the artist name from a dropdown box. The problem is that I don't know how to take the value from the dropdown box.The new album is saved without any artist_id. I am using Sqlite3 and Sequel.

DB.create_table?(:artists) do
        primary_key :id
        String :name
    end
    DB.create_table?(:albums) do
        primary_key :id
        foreign_key :artist_id, :artists
        String :title
    end


class Album<Sequel::Model
  many_to_one :artist
end
class Artist<Sequel::Model
  one_to_many :albums
end

get '/albums/new' do
@album=Album.new
@artists=Artist.all
slim :new_album
end

post '/albums' do
album=Album.create(params[:album])
redirect to('/albums/#{album.id}')
end

My slim files are

/new_album.slim

h1 Add A New Album
form method="POST" action="/albums"
  == slim :album_form



/album_form.slim

label for="title" Title:
input#title type="text" name="album[title]" value="#{@album.title}"
label Performed by:
select value="#{@album.artist_id}" 
  - if @artists.any?
    ul#artists
      -@artists.each do |artist|
        <option name="song[artist_id]" value="#{artist.id}"> #{artist.name}</option>

input type="submit" value="Save Album"

The dropdown box is created but I don't know how to save my choice in the database. Any suggestions are welcome.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
magmike
  • 473
  • 1
  • 5
  • 18

1 Answers1

0

By changing this

select value="#{@album.artist_id}" 

to this

select name="album[artist_id]"

the value is passed to the database

magmike
  • 473
  • 1
  • 5
  • 18