Skip to content

Instantly share code, notes, and snippets.

@msmithstubbs
Last active June 25, 2019 05:24
Show Gist options
  • Save msmithstubbs/ab25619b15cd71758a58ef051982e89e to your computer and use it in GitHub Desktop.
Save msmithstubbs/ab25619b15cd71758a58ef051982e89e to your computer and use it in GitHub Desktop.
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
<%= 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="&#x2713;" /><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>
@msmithstubbs
Copy link
Author

msmithstubbs commented Jun 24, 2019

<%= form_with local: true do %>
<% end %>

<form action="/" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value=“LIRzmJeQd11…” /></form>

<%= form_with model: @book do %>
<% end %>


<%= form_with model: Book.new do |f| %>
  <%= f.text_field :title %>
<% end %>

<form action="/books" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value=“HkpkaIo23…” />

<%= form.select :genre, options_for_select(["Please select...", "Memoir", "Fantasy", "Romance"]) %>




<select name="book[genre]" id="book_genre"><option value="Please select...">Please select...</option>
  <option value="Memoir">Memoir</option>
  <option value="Fantasy">Fantasy</option>
  <option value="Romance">Romance</option>
  <option value="Non-fiction">Non-fiction</option></select>



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment