0
我有一個深層嵌套的資源。目前一切正常,除了當我創造一首歌曲時。由於某些原因,它不會將ARTIST_ID存儲在數據庫中(顯示爲NIL)。有人可以幫助我,我是一個新手。深嵌套的資源
第一嵌套存儲在專輯表中的ARTIST_ID ...
的routes.rb
resources :artists do
resources :albums do
resources :songs
end
end
SONGS_CONTROLLER
class SongsController < ApplicationController
respond_to :html, :js
def index
@artist = Artist.find(params[:artist_id])
@album = Album.find(params[:album_id])
@songs = @album.songs.all
end
def create
@artist = Artist.find(params[:artist_id])
@album = Album.find(params[:album_id])
@song = @album.songs.create(params[:song])
if @song.save
redirect_to artist_album_songs_url
flash[:success] = "Song Created."
else
render 'new'
end
end
def new
@artist = Artist.find(params[:artist_id])
@album = Album.find(params[:album_id])
@song = Song.new
end
end
模型
class Artist < ActiveRecord::Base
attr_accessible :name
has_many :albums
has_many :songs
end
class Album < ActiveRecord::Base
attr_accessible :name, :artist_id
belongs_to :artist
has_many :songs
end
class Song < ActiveRecord::Base
attr_accessible :name, :album_id, :artist_id
belongs_to :albums
end
VIEW(CREATE ,對於歌曲)
<div class="container-fluid">
<div class="row-fluid">
<%= form_for ([@artist,@album, @song]), :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_field :name, placeholder: "name", :class => "input-xlarge"%>
<%= f.submit "Create Song", class: "btn btn-primary"%>
<% end %>
</div>
</div>
真棒謝謝! :) –