Last active
December 17, 2015 10:19
-
-
Save adamyonk/5593868 to your computer and use it in GitHub Desktop.
Rails 4, Strong Parameters, and Nested Forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= form_for @album do |f| | |
= f.text_field :title | |
= f.fields_for :songs do |s| | |
= s.text_field :name | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Album < ActiveRecord::Base | |
has_many :songs | |
accepts_nested_attributes_for :songs, allow_destroy: true | |
... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AlbumsController < ApplicationController | |
... | |
private | |
def album_params | |
params.require(:album).permit(:title, songs_attributes: [:id, :_destroy, :name]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Song < ActiveRecord::Base | |
belongs_to :album | |
... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment