-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
288 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
class MessagesController < ApplicationController | ||
before_action :set_message, only: %i[ show edit update destroy ] | ||
|
||
# GET /messages or /messages.json | ||
def index | ||
@messages = Message.all | ||
end | ||
|
||
# GET /messages/1 or /messages/1.json | ||
def show | ||
end | ||
|
||
# GET /messages/new | ||
def new | ||
@message = Message.new | ||
end | ||
|
||
# GET /messages/1/edit | ||
def edit | ||
end | ||
|
||
# POST /messages or /messages.json | ||
def create | ||
@message = Message.new(message_params) | ||
|
||
respond_to do |format| | ||
if @message.save | ||
format.html { redirect_to @message, notice: "Message was successfully created." } | ||
format.json { render :show, status: :created, location: @message } | ||
else | ||
format.html { render :new, status: :unprocessable_entity } | ||
format.json { render json: @message.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PATCH/PUT /messages/1 or /messages/1.json | ||
def update | ||
respond_to do |format| | ||
if @message.update(message_params) | ||
format.html { redirect_to @message, notice: "Message was successfully updated." } | ||
format.json { render :show, status: :ok, location: @message } | ||
else | ||
format.html { render :edit, status: :unprocessable_entity } | ||
format.json { render json: @message.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /messages/1 or /messages/1.json | ||
def destroy | ||
@message.destroy | ||
respond_to do |format| | ||
format.html { redirect_to messages_url, notice: "Message was successfully destroyed." } | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
# Use callbacks to share common setup or constraints between actions. | ||
def set_message | ||
@message = Message.find(params[:id]) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def message_params | ||
params.require(:message).permit(:body) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module MessagesHelper | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Message < ApplicationRecord | ||
validates :body, presence: true | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<%= form_with(model: message) do |form| %> | ||
<% if message.errors.any? %> | ||
<div id="error_explanation"> | ||
<h2><%= pluralize(message.errors.count, "error") %> prohibited this message from being saved:</h2> | ||
|
||
<ul> | ||
<% message.errors.each do |error| %> | ||
<li><%= error.full_message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<div class="field"> | ||
<%= form.label :body %> | ||
<%= form.text_area :body %> | ||
</div> | ||
|
||
<div class="actions"> | ||
<%= form.submit %> | ||
</div> | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<div id="<%= dom_id message %>" class="scaffold_record"> | ||
<p> | ||
<strong>Body:</strong> | ||
<%= message.body %> | ||
</p> | ||
|
||
<p> | ||
<%= link_to "Show this message", message %> | ||
</p> | ||
</div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
json.extract! message, :id, :body, :created_at, :updated_at | ||
json.url message_url(message, format: :json) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<h1>Editing message</h1> | ||
|
||
<%= render "form", message: @message %> | ||
|
||
<br> | ||
|
||
<div> | ||
<%= link_to "Show this message", @message %> | | ||
<%= link_to "Back to messages", messages_path %> | ||
</div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<h1>Message</h1> | ||
|
||
<div id="messages"> | ||
<%= render @messages %> | ||
</div> | ||
|
||
<%= link_to "New message", new_message_path %> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.array! @messages, partial: "messages/message", as: :message |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h1>New message</h1> | ||
|
||
<%= render "form", message: @message %> | ||
|
||
<br> | ||
|
||
<div> | ||
<%= link_to "Back to messages", messages_path %> | ||
</div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<%= render @message %> | ||
|
||
<div> | ||
<%= link_to "Edit this message", edit_message_path(@message) %> | | ||
<%= link_to "Back to messages", messages_path %> | ||
<%= button_to "Destroy this message", message_path(@message), method: :delete %> | ||
</div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.partial! "messages/message", message: @message |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
Rails.application.routes.draw do | ||
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html | ||
|
||
# Almost every application defines a route for the root path ("/") at the top of this file. | ||
# root "articles#index" | ||
root "messages#index" | ||
resources :messages | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateMessages < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :messages do |t| | ||
t.text :body | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require "test_helper" | ||
|
||
class MessagesControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@message = messages(:one) | ||
end | ||
|
||
test "should get index" do | ||
get messages_url | ||
assert_response :success | ||
end | ||
|
||
test "should get new" do | ||
get new_message_url | ||
assert_response :success | ||
end | ||
|
||
test "should create message" do | ||
assert_difference("Message.count") do | ||
post messages_url, params: { message: { body: @message.body } } | ||
end | ||
|
||
assert_redirected_to message_url(Message.last) | ||
end | ||
|
||
test "should show message" do | ||
get message_url(@message) | ||
assert_response :success | ||
end | ||
|
||
test "should get edit" do | ||
get edit_message_url(@message) | ||
assert_response :success | ||
end | ||
|
||
test "should update message" do | ||
patch message_url(@message), params: { message: { body: @message.body } } | ||
assert_redirected_to message_url(@message) | ||
end | ||
|
||
test "should destroy message" do | ||
assert_difference("Message.count", -1) do | ||
delete message_url(@message) | ||
end | ||
|
||
assert_redirected_to messages_url | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
one: | ||
body: MyText | ||
|
||
two: | ||
body: MyText |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "test_helper" | ||
|
||
class MessageTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "application_system_test_case" | ||
|
||
class MessagesTest < ApplicationSystemTestCase | ||
setup do | ||
@message = messages(:one) | ||
end | ||
|
||
test "visiting the index" do | ||
visit messages_url | ||
assert_selector "h1", text: "Messages" | ||
end | ||
|
||
test "should create Message" do | ||
visit messages_url | ||
click_on "New Message" | ||
|
||
fill_in "Body", with: @message.body | ||
click_on "Create Message" | ||
|
||
assert_text "Message was successfully created" | ||
click_on "Back" | ||
end | ||
|
||
test "should update Message" do | ||
visit messages_url | ||
click_on "Edit", match: :first | ||
|
||
fill_in "Body", with: @message.body | ||
click_on "Update Message" | ||
|
||
assert_text "Message was successfully updated" | ||
click_on "Back" | ||
end | ||
|
||
test "should destroy Message" do | ||
visit messages_url | ||
page.accept_confirm do | ||
click_on "Destroy", match: :first | ||
end | ||
|
||
assert_text "Message was successfully destroyed" | ||
end | ||
end |