Rails 4嵌套表单字段未保存在数据库中

我有表格用于输入专辑以及属于专辑的歌曲。 相册正在保存到数据库,但单个歌曲不会保存。

专辑模型

class Album  :destroy accepts_nested_attributes_for :songs has_attached_file :image, :styles => { :large => "500x500>", :medium => "300x300>", :thumb => "100x100>" } validates :image, presence: true validates :title, presence: true extend FriendlyId friendly_id :title, use: :slugged end 

歌曲模型

 class Song < ActiveRecord::Base belongs_to :album extend FriendlyId friendly_id :title, use: :slugged end 

专辑控制器

 class AlbumsController  params[:page], :per_page => 8) end def show end def new @album = current_user.albums.build 3.times { @album.songs.build } end def edit end def create @album = current_user.albums.build(album_params) if @album.save redirect_to @album, notice: 'Album was successfully created.' else render action: 'new' end end def update if @album.update(album_params) redirect_to @album, notice: 'Album was successfully updated.' else render action: 'edit' end end def destroy @album.destroy redirect_to albums_url end private def verify_is_admin (current_user.nil?) ? redirect_to(root_path) : (redirect_to(root_path) unless current_user.admin?) end # Use callbacks to share common setup or constraints between actions. def set_album @album = Album.find_by_slug(params[:id]) end def correct_user @album = current_user.albums.find_by(id: params[:id]) redirect_to albums_path, notice: "Not authorized to edit this album" if @album.nil? end # Never trust parameters from the scary internet, only allow the white list through. def album_params params.require(:album).permit(:title, :image, :embed, :embed_html) end end 

歌曲控制器

 class SongsController < ApplicationController before_action :set_song, only: [:show, :edit, :update, :destroy] def index @songs = Song.all end def show end def new @song = Song.new end def edit end def create @song = Song.new(song_params) respond_to do |format| if @song.save format.html { redirect_to @song, notice: 'Song was successfully created.' } format.json { render action: 'show', status: :created, location: @song } else format.html { render action: 'new' } format.json { render json: @song.errors, status: :unprocessable_entity } end end end def update respond_to do |format| if @song.update(song_params) format.html { redirect_to @song, notice: 'Song was successfully updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @song.errors, status: :unprocessable_entity } end end end def destroy @song.destroy respond_to do |format| format.html { redirect_to songs_url } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_song @song = Song.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def song_params params.require(:song).permit(:title) end end 

这是新专辑视图的表单

   

prohibited this album from being saved:


3 %>

你需要告诉你关于歌曲参数的强大参数:

 def album_params params.require(:album).permit(:title, :image, :embed, :embed_html, :songs_attributes => [:id, :_destroy, :title]) end 

不要忘记包含您希望能够更新的所有歌曲属性。