Last active
June 25, 2019 05:24
-
-
Save msmithstubbs/ab25619b15cd71758a58ef051982e89e to your computer and use it in GitHub Desktop.
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 Book < ApplicationRecord | |
validates :title, presence: true | |
end | |
book = Book.new | |
> <Book id: nil, title: nil, author_id: nil, created_at: nil, updated_at: nil, genre: nil> | |
book.valid? | |
> false | |
book.errors | |
> #<ActiveModel::Errors:0x00007feab2963530 @base=#<Book id: nil, title: nil, author_id: nil, created_at: nil, updated_at: nil, genre: nil>, @messages={:title=>["can't be blank"]}, @details={:title=>[{:error=>:blank}]}> | |
book.save | |
> false | |
book.title = "American Gods" | |
book.valid? | |
> true | |
book.save | |
> true | |
validates :postcode, length: { is: 4 } # exactly 4 characters | |
validates :username, length: { minimum: 3 } | |
validates :price, numericality: true | |
validates :username, uniqueness: true | |
book.errors.full_messages | |
=> ["Title can't be blank"] | |
@book = Book.new(title: params[:book][:title]) | |
if @book.save | |
flash[:alert] = "Your book has been saved" | |
redirect_to "/" | |
else | |
flash[:alert] = @book.errors.full_messages.join() | |
render "edit" | |
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
<%= form_with model: @book do |f| %> | |
<%= f.text_field :title %> | |
<% end %> | |
<form action="/books/13" accept-charset="UTF-8" data-remote="true" method="post"> <input name="utf8" type="hidden" value="✓" /><input type="hidden" name="_method" value="patch" /><input type="hidden" name="authenticity_token" value=“tm8UJDkWoOE…” /> <input type="text" value="Neverworld" name="book[title]" id="book_title" /> </form> |
Author
msmithstubbs
commented
Jun 24, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment