Skip to content

Commit 91a7e07

Browse files
committed
Apply a change
1 parent 9d90832 commit 91a7e07

File tree

10 files changed

+147
-15
lines changed

10 files changed

+147
-15
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class BooksController < ApplicationController
2+
before_action :set_book, only: [:show, :update, :destroy]
3+
4+
# GET /books
5+
def index
6+
@books = Book.all
7+
8+
render json: @books
9+
end
10+
11+
# GET /books/1
12+
def show
13+
render json: @book
14+
end
15+
16+
# POST /books
17+
def create
18+
@book = Book.new(book_params)
19+
20+
if @book.save
21+
render json: @book, status: :created, location: @book
22+
else
23+
render json: @book.errors, status: :unprocessable_entity
24+
end
25+
end
26+
27+
# PATCH/PUT /books/1
28+
def update
29+
if @book.update(book_params)
30+
render json: @book
31+
else
32+
render json: @book.errors, status: :unprocessable_entity
33+
end
34+
end
35+
36+
# DELETE /books/1
37+
def destroy
38+
@book.destroy
39+
end
40+
41+
private
42+
# Use callbacks to share common setup or constraints between actions.
43+
def set_book
44+
@book = Book.find(params[:id])
45+
end
46+
47+
# Only allow a list of trusted parameters through.
48+
def book_params
49+
params.require(:book).permit(:name, :author, :pages)
50+
end
51+
end

app/controllers/homes_controller.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ def index
66
end
77

88
def new
9-
object = User.new(
10-
:ip => "192.168.1.50",
11-
:key_user => "key1",
12-
:key_ip => "key2",
13-
:key_license => "key3",
14-
:datetime => 123,
15-
:datetime_expire => nil,
16-
:use_first => nil,
17-
:use_last => nil,
18-
:is_active => 1
19-
)
20-
object.save
9+
render json: params
10+
return
11+
object = User.new(
12+
:ip => "192.168.1.50",
13+
:key_user => "key1",
14+
:key_ip => "key2",
15+
:key_license => "key3",
16+
:datetime => 123,
17+
:datetime_expire => nil,
18+
:use_first => nil,
19+
:use_last => nil,
20+
:is_active => 1
21+
)
22+
object.save
2123
end
2224

2325
end

app/models/book.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Book < ApplicationRecord
2+
end

config/routes.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Rails.application.routes.draw do
2+
resources :books
23
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
34
root to: 'homes#index'
4-
get 'new', to: 'homes#new'
5-
5+
# get 'new', to: 'homes#new'
6+
get '/new' => 'homes#new', :defaults => { :name => 'Max' }
7+
get '/new/:name' => 'homes#new'
68
end

db/development.sqlite3

4 KB
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreateBooks < ActiveRecord::Migration[6.1]
2+
def change
3+
create_table :books do |t|
4+
t.string :name
5+
t.string :author
6+
t.string :pages
7+
8+
t.timestamps
9+
end
10+
end
11+
end

db/schema.rb

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require "test_helper"
2+
3+
class BooksControllerTest < ActionDispatch::IntegrationTest
4+
setup do
5+
@book = books(:one)
6+
end
7+
8+
test "should get index" do
9+
get books_url, as: :json
10+
assert_response :success
11+
end
12+
13+
test "should create book" do
14+
assert_difference('Book.count') do
15+
post books_url, params: { book: { author: @book.author, name: @book.name, pages: @book.pages } }, as: :json
16+
end
17+
18+
assert_response 201
19+
end
20+
21+
test "should show book" do
22+
get book_url(@book), as: :json
23+
assert_response :success
24+
end
25+
26+
test "should update book" do
27+
patch book_url(@book), params: { book: { author: @book.author, name: @book.name, pages: @book.pages } }, as: :json
28+
assert_response 200
29+
end
30+
31+
test "should destroy book" do
32+
assert_difference('Book.count', -1) do
33+
delete book_url(@book), as: :json
34+
end
35+
36+
assert_response 204
37+
end
38+
end

test/fixtures/books.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
3+
one:
4+
name: MyString
5+
author: MyString
6+
pages:
7+
8+
two:
9+
name: MyString
10+
author: MyString
11+
pages:

test/models/book_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "test_helper"
2+
3+
class BookTest < ActiveSupport::TestCase
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

0 commit comments

Comments
 (0)