0
我在製作電影票務應用程序,其中有電影,每部電影都有很多放映時間。保存與現有模型的關聯
這是我的電影類:
class Movie < ActiveRecord::Base
has_many :showtimes, dependent: :destroy
end
和Showtime類
class Showtime < ActiveRecord::Base
belongs_to :movie
end
在我的欣欣形式我有以下領域
<%= f.collection_select :movie, Movie.all, :id, :title %>
在我的欣欣控制器做我有以下創建方法
def create
@showtime = Showtime.new(showtime_params)
if @showtime.save
redirect_to @showtime, notice: 'Showtime was successfully created.'
else
render :new
end
end
def showtime_params
params.require(:showtime).permit(:movie_id, :start_time)
end
我這是保存該關聯的正確方法?
這樣做的好處是什麼? –
只是個人喜好。我發現比查找collection_for_select的順序或參數更容易記住。 select的選項可以採用一維數組,或者,如果要顯示不同的值(名稱而不是id,如上所示),則可以使用二維數組。我想,我不喜歡需要大量參數的方法。不管哪種方式都行得通。 – toddmetheny