Skip to content

Instantly share code, notes, and snippets.

@lmansur
Created November 16, 2016 04:33
Show Gist options
  • Save lmansur/f2f2a6a8bbba44a90f365c3e73742071 to your computer and use it in GitHub Desktop.
Save lmansur/f2f2a6a8bbba44a90f365c3e73742071 to your computer and use it in GitHub Desktop.
### author.rb
class Author < ApplicationRecord
has_many :books
validates :name, presence: true
end
### authors_controllers.rb
class AuthorsController < ApplicationController
before_action :set_author
def new
end
def create
if @author.save
redirect_to root_path
else
render 'new'
end
end
private
def author_params
params.require(:author).permit(:name, :nationality)
end
def set_author
@author = Author.new
end
end
### authors/new.html.haml
%h2 Add New Author
= render 'form'
### authors/_form.html.haml
= simple_form_for @author do |f|
= f.input :name, label: 'Author Name'
= f.input :nationality, label: 'Country'
= f.button :submit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment