Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class ArticlesController < ApplicationController
before_action :set_article, only: %i[ show edit update destroy ]

# GET /articles or /articles.json
def index
@articles = Article.all
end

# GET /articles/1 or /articles/1.json
def show
end

# GET /articles/new
def new
@article = Article.new
end

# GET /articles/1/edit
def edit
end

# POST /articles or /articles.json
def create
@article = Article.new(article_params)

respond_to do |format|
if @article.save
format.html { redirect_to article_url(@article), notice: "Article was successfully created." }
format.json { render :show, status: :created, location: @article }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /articles/1 or /articles/1.json
def update
respond_to do |format|
if @article.update(article_params)
format.html { redirect_to article_url(@article), notice: "Article was successfully updated." }
format.json { render :show, status: :ok, location: @article }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end

# DELETE /articles/1 or /articles/1.json
def destroy
@article.destroy!

respond_to do |format|
format.html { redirect_to articles_url, notice: "Article was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_article
@article = Article.find(params[:id])
end

# Only allow a list of trusted parameters through.
def article_params
params.require(:article).permit(:title, :body)
end
end
2 changes: 2 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Article < ApplicationRecord
end
12 changes: 12 additions & 0 deletions app/views/articles/_article.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div id="<%= dom_id article %>">
<p>
<strong>Title:</strong>
<%= article.title %>
</p>

<p>
<strong>Body:</strong>
<%= article.body %>
</p>

</div>
2 changes: 2 additions & 0 deletions app/views/articles/_article.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! article, :id, :title, :body, :created_at, :updated_at
json.url article_url(article, format: :json)
14 changes: 14 additions & 0 deletions app/views/articles/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

<%= simple_form_for(@article) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

<div class="form-inputs">
<%= f.input :title %>
<%= f.input :body %>
</div>

<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
10 changes: 10 additions & 0 deletions app/views/articles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Editing article</h1>

<%= render "form", article: @article %>

<br>

<div>
<%= link_to "Show this article", @article %> |
<%= link_to "Back to articles", articles_path %>
</div>
14 changes: 14 additions & 0 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>

<h1>Articles</h1>

<div id="articles">
<% @articles.each do |article| %>
<%= render article %>
<p>
<%= link_to "Show this article", article %>
</p>
<% end %>
</div>

<%= link_to "New article", new_article_path %>
1 change: 1 addition & 0 deletions app/views/articles/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @articles, partial: "articles/article", as: :article
9 changes: 9 additions & 0 deletions app/views/articles/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>New article</h1>

<%= render "form", article: @article %>

<br>

<div>
<%= link_to "Back to articles", articles_path %>
</div>
10 changes: 10 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>

<%= render @article %>

<div>
<%= link_to "Edit this article", edit_article_path(@article) %> |
<%= link_to "Back to articles", articles_path %>

<%= button_to "Destroy this article", @article, method: :delete %>
</div>
1 change: 1 addition & 0 deletions app/views/articles/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "articles/article", article: @article
6 changes: 3 additions & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@
price: rand(2000..5000),
duration: rand(2..10),
user: User.all.sample,
latitude: 2.380061 + rand(0.01..0.06),
longitude: 48.8649574 + rand(0.1..0.6),
latitude: 35.652832 + rand(0.01..0.06),
longitude: 139.839478 + rand(0.1..0.6)
)
image_url[experience[:title]].each_with_index do |url, index|
file = URI.open(url)
Expand All @@ -146,7 +146,7 @@
user: User.all.sample,
date: Date.today,
status: rand(1..3) # needs to be at least 6 characters
)
)
end

puts "Created #{Booking.count} bookings!"
Expand Down
48 changes: 48 additions & 0 deletions test/controllers/articles_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "test_helper"

class ArticlesControllerTest < ActionDispatch::IntegrationTest
setup do
@article = articles(:one)
end

test "should get index" do
get articles_url
assert_response :success
end

test "should get new" do
get new_article_url
assert_response :success
end

test "should create article" do
assert_difference("Article.count") do
post articles_url, params: { article: { body: @article.body, title: @article.title } }
end

assert_redirected_to article_url(Article.last)
end

test "should show article" do
get article_url(@article)
assert_response :success
end

test "should get edit" do
get edit_article_url(@article)
assert_response :success
end

test "should update article" do
patch article_url(@article), params: { article: { body: @article.body, title: @article.title } }
assert_redirected_to article_url(@article)
end

test "should destroy article" do
assert_difference("Article.count", -1) do
delete article_url(@article)
end

assert_redirected_to articles_url
end
end
7 changes: 7 additions & 0 deletions test/models/article_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class ArticleTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
43 changes: 43 additions & 0 deletions test/system/articles_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "application_system_test_case"

class ArticlesTest < ApplicationSystemTestCase
setup do
@article = articles(:one)
end

test "visiting the index" do
visit articles_url
assert_selector "h1", text: "Articles"
end

test "should create article" do
visit articles_url
click_on "New article"

fill_in "Body", with: @article.body
fill_in "Title", with: @article.title
click_on "Create Article"

assert_text "Article was successfully created"
click_on "Back"
end

test "should update Article" do
visit article_url(@article)
click_on "Edit this article", match: :first

fill_in "Body", with: @article.body
fill_in "Title", with: @article.title
click_on "Update Article"

assert_text "Article was successfully updated"
click_on "Back"
end

test "should destroy Article" do
visit article_url(@article)
click_on "Destroy this article", match: :first

assert_text "Article was successfully destroyed"
end
end