-
-
Save lmansur/f2f2a6a8bbba44a90f365c3e73742071 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
### 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