0

ユーザーがアルバムのタイトルを付け、ドロップダウン ボックスからアーティスト名を選択するフォームを作成しようとしています。問題は、ドロップダウン ボックスから値を取得する方法がわからないことです。新しいアルバムは、artist_id なしで保存されます。Sqlite3 と 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

私のスリムファイルは

/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"

ドロップダウン ボックスが作成されましたが、選択内容をデータベースに保存する方法がわかりません。どんな提案でも大歓迎です。

4

1 に答える 1