Rails 4 嵌套表单字段未保存在数据库中 [英] Rails 4 Nested form fields not saving in database

查看:45
本文介绍了Rails 4 嵌套表单字段未保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有输入专辑的表格,还有属于专辑的歌曲.专辑正在保存到数据库,但个别歌曲没有保存.

相册模型

班级相册:破坏accepts_nested_attributes_for :songshas_attached_file :image, :styles =>{:大=>"500x500>", :medium =>"300x300>", :thumb =>100x100"}验证:图像,存在:真验证:标题,存在:真扩展 FriendlyIdFriendly_id :title, 使用: :slugged结尾

歌曲模型

class Song 

相册控制器

class AlbumsController <应用控制器before_action :set_album, only: [:show, :edit, :update, :destroy]before_action :authenticate_user!, 除了: [:index, :show]before_filter :verify_is_admin, 除了: [:index, :show]定义索引@albums = Album.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 8)结尾高清秀结尾定义新@album = current_user.albums.build3.times { @album.songs.build }结尾定义编辑结尾定义创建@album = current_user.albums.build(album_params)如果@album.saveredirect_to @album,注意:'相册已成功创建.'别的渲染动作:新"结尾结尾定义更新如果@album.update(album_params)redirect_to @album,注意:'相册已成功更新.'别的渲染动作:'编辑'结尾结尾销毁@album.destroy重定向到相册_url结尾私人的def verify_is_admin(current_user.nil?)?redirect_to(root_path) : (redirect_to(root_path) 除非 current_user.admin?)结尾# 使用回调在动作之间共享公共设置或约束.def set_album@album = Album.find_by_slug(params[:id])结尾定义正确_用户@album = current_user.albums.find_by(id: params[:id])重定向到专辑路径,请注意:如果@album.nil,无权编辑此专辑"?结尾# 永远不要相信来自可怕互联网的参数,只允许白名单通过.定义专辑参数params.require(:album).permit(:title, :image, :embed, :embed_html)结尾结尾

歌曲控制器

class SongsController <应用控制器before_action :set_song, only: [:show, :edit, :update, :destroy]定义索引@songs = Song.all结尾高清秀结尾定义新@song = Song.new结尾定义编辑结尾定义创建@song = Song.new(song_params)response_to do |格式|如果@song.saveformat.html { redirect_to @song,注意:'歌曲创建成功.'}format.json { 渲染动作:'show',状态::已创建,位置:@song }别的format.html { 渲染动作:'新' }format.json { 渲染 json: @song.errors, 状态: :unprocessable_entity }结尾结尾结尾定义更新response_to do |格式|如果@song.update(song_params)format.html { redirect_to @song,注意:'歌曲更新成功.'}format.json { 头:no_content }别的format.html { 渲染动作:'编辑' }format.json { 渲染 json: @song.errors, 状态: :unprocessable_entity }结尾结尾结尾销毁@song.destroyresponse_to do |格式|format.html { redirect_to song_url }format.json { 头:no_content }结尾结尾私人的# 使用回调在动作之间共享公共设置或约束.def set_song@song = Song.find(params[:id])结尾# 永远不要相信来自可怕互联网的参数,只允许白名单通过.定义歌曲参数params.require(:song).permit(:title)结尾结尾

这是新专辑视图的表单

<%= form_for @album, html: { multipart: true } do |f|%><% if @album.errors.any?%><div id="error_explanation"><h2><%=pluralize(@album.errors.count, "error") %>禁止保存此专辑:</h2><ul><% @album.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>

<%结束%><div class="form-group"><%= f.label :Artwork %><%= f.file_field :image, class: "form-control" %>

<div class="form-group"><%= f.label :title %><%= f.text_field :title, class: "form-control" %>

<div class="form-group"><%= f.fields_for :songs do |builder|%><字段集><%= builder.label :title, "Song" %><br/><%= builder.text_field :title, :rows =>3%></fieldset><%结束%>

<div class="form-group"><%= f.submit class: "btn btn-primary" %>

<%结束%>

解决方案

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

def album_paramsparams.require(:album).permit(:title, :image, :embed, :embed_html,:songs_attributes =>[:id, :_destroy, :title])结尾

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

I have form for inputing albums and also the songs that belong to the album. Albums are saving to database, but individual songs are not saving.

Albums model

class Album < ActiveRecord::Base
    belongs_to :user
    has_many :songs, :dependent => :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

Songs model

class Song < ActiveRecord::Base
    belongs_to :album

    extend FriendlyId
    friendly_id :title, use: :slugged
end

Albums controller

class AlbumsController < ApplicationController
  before_action :set_album, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]
  before_filter :verify_is_admin, except: [:index, :show]


  def index
    @albums = Album.all.order("created_at DESC").paginate(:page => 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

Songs controller

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

and here is the form for the new album view

<%= form_for @album, html: { multipart: true } do |f| %>
  <% if @album.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@album.errors.count, "error") %> prohibited this album from being saved:</h2>

      <ul>
      <% @album.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="form-group">
    <%= f.label :Artwork %>
    <%= f.file_field :image, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :title %>
    <%= f.text_field :title, class: "form-control" %>
  </div>

  <div class="form-group">
  <%= f.fields_for :songs do |builder| %>
    <fieldset>
      <%= builder.label :title, "Song" %><br />
      <%= builder.text_field :title, :rows => 3 %>
    </fieldset>
  <% end %>
  </div>

  <div class="form-group">
    <%= f.submit class: "btn btn-primary" %>
  </div>

<% end %>

解决方案

You need to tell your strong parameters about the song params:

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

Don't forget to include all of the song's attributes that you want to be able to update.

这篇关于Rails 4 嵌套表单字段未保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆