Rails嵌套表单属性未保存

时间:2022-03-25 15:56:45

I've already looked through every other * for this issue, but none of the solutions have fixed this. My elements in a nested_form are not being saved in the database. I've also made sure that all model associations are correct. I've been trying to fix this for nearly 8 hours now, and would really appreciate some help, especially considering every other solution hasn't worked.

我已经查看了此问题的所有其他*,但没有一个解决方案已修复此问题。我在nested_form中的元素没有保存在数据库中。我还确保所有模型关联都是正确的。我一直在努力解决这个问题近8个小时,并且非常感谢一些帮助,特别是考虑到其他所有解决方案都没有奏效。

Basically, I have a Playlist model that contains multiple Song models. I'm trying to use a nested_form to add the Song models to the Playlist. However, none of the Songs are ever being saved. I apologize if my methods are misguides, as I'm still fairly new to Rails.

基本上,我有一个包含多个Song模型的播放列表模型。我正在尝试使用nested_form将Song模型添加到播放列表中。然而,没有一首歌被保存。如果我的方法是错误的,我道歉,因为我还是Rails的新手。

GitHub Repo:https://github.com/nsalesky/Ultra-Music

GitHub Repo:https://github.com/nsalesky/Ultra-Music

playlists_controller.rb

playlists_controller.rb

def index
    @user = current_user
    @playlists = @user.playlists
end

def show
    @user = current_user
    @playlist = @user.playlists.find(params[:id])
end

def new
    @playlist = Playlist.new

    #I was told to do this
    @playlist.songs.build
end

def create
    @user = current_user
    @playlist = @user.playlists.create(playlist_params)

    if @playlist.save
        redirect_to @playlist
    else
        render :action => 'new'
    end
end

def edit
    @playlist = current_user.playlists.find(params[:id])
end

def update
    @user = current_user
    @playlist = @user.playlists.find(params[:id])

    if @playlist.update_attributes(playlist_params)
        redirect_to @playlist
    else
        render :action => 'edit'
    end
end

def destroy
    @user = current_user
    @playlist = @user.playlists.find(params[:id])
    @playlist.destroy
    redirect_to playlists_path(@user.playlists)
end

private
    def playlist_params
        params.require(:playlist).permit(:name, :description, songs_attributes: [:id, :name, :link, :_destroy])
    end

playlist.rb

playlist.rb

belongs_to :user
  has_many :songs, dependent: :destroy
  accepts_nested_attributes_for :songs, :allow_destroy => true, :reject_if => lambda { |a| a[:content].blank? }


  validates :name, presence: true
  validates_associated :songs, presence: true

_form.html.erb

_form.html.erb

<%= nested_form_for @playlist do |f| %>

    <div>
        <%= f.label :name %>
        <%= f.text_field :name %>
    </div>

    <div>
        <%= f.label :description %>
        <%= f.text_field :description %>
    </div>

    <!--<div>
        <button type="button" id="addsong">Add Song</button><br>
        <button type="button" id="removesong">Remove Song</button><br>
    </div> !-->

    <div>
        <%= f.fields_for :songs do |song_form| %>
            <%= song_form.text_field :name %>
            <%= song_form.text_field :link %>
            <%= song_form.link_to_remove "Remove Song" %>
        <% end %>
        <p><%= f.link_to_add "Add Song", :songs %></p>
    </div>

    <div>
        <%= f.submit %>
    </div>
<% end %>

1 个解决方案

#1


1  

In your playlist.rb, you wrote:

在你的playlist.rb中,你写道:

:reject_if => lambda { |a| a[:content].blank? }

:reject_if => lambda {| a |一[:内容] .blank? }

Here the block parameter |a| stands for attributes of a specific song. So a[:attribute] relates to a single attribute. The problem is your Song doesn't have a :content attribute. So this a[:content].blank? will always be true, means you would be rejected building a song.

这里块参数| a |代表特定歌曲的属性。所以[:attribute]与单个属性相关。问题是你的歌没有:content属性。那么这个[:content] .blank?将永远是真的,意味着你会被拒绝建立一首歌。

Just change a[:content] to a valid attribute such as a[:name]

只需将[:content]更改为有效属性,例如[:name]

#1


1  

In your playlist.rb, you wrote:

在你的playlist.rb中,你写道:

:reject_if => lambda { |a| a[:content].blank? }

:reject_if => lambda {| a |一[:内容] .blank? }

Here the block parameter |a| stands for attributes of a specific song. So a[:attribute] relates to a single attribute. The problem is your Song doesn't have a :content attribute. So this a[:content].blank? will always be true, means you would be rejected building a song.

这里块参数| a |代表特定歌曲的属性。所以[:attribute]与单个属性相关。问题是你的歌没有:content属性。那么这个[:content] .blank?将永远是真的,意味着你会被拒绝建立一首歌。

Just change a[:content] to a valid attribute such as a[:name]

只需将[:content]更改为有效属性,例如[:name]