-
Notifications
You must be signed in to change notification settings - Fork 8
Basic CRUD
JP Barbosa edited this page Jan 14, 2016
·
4 revisions
rails g scaffold Article title:string content:text --no-helper --no-assets
rake db:migrate
open http://localhost:3000/articles
nano app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
skip_before_action :verify_authenticity_token, if: :json_request?
protected
def json_request?
request.format.json?
end
end
curl -H "Accept: application/json" \
http://localhost:3000/articles \
--data "article[title]=My Article" \
--data "article[content]=My Article Content"
open http://localhost:3000/articles
nano app/models/article.rb
class Article < ActiveRecord::Base
validates :title, presence: true, length: {
minimum: 10,
maximum: 100,
}
validates :content, presence: true, length: {
minimum: 10,
maximum: 1000,
}
end
nano test/fixtures/articles.yml
one:
title: My title has more than 10 characters
content: My text has more than 10 characters
curl -H "Accept: application/json" \
http://localhost:3000/articles \
--data "article[title]=A" \
--data "article[content]=B"
{"title":["is too short (minimum is 10 characters)"]}...
curl -H "Accept: application/json" \
http://localhost:3000/articles \
--data "article[title]=Article Title" \
--data "article[content]=Article Content"
{"id":2,"title":"Article Title","content":"Article Content"...
git add .
git commit -m "Add articles CRUD files"