Commit 229315a1 authored by sftblw's avatar sftblw
Browse files

rails g scaffold sentence body:text account:references

parent 15ffefc4
class SentencesController < ApplicationController
before_action :set_sentence, only: %i[ show edit update destroy ]
# GET /sentences or /sentences.json
def index
@sentences = Sentence.all
end
# GET /sentences/1 or /sentences/1.json
def show
end
# GET /sentences/new
def new
@sentence = Sentence.new
end
# GET /sentences/1/edit
def edit
end
# POST /sentences or /sentences.json
def create
@sentence = Sentence.new(sentence_params)
respond_to do |format|
if @sentence.save
format.html { redirect_to sentence_url(@sentence), notice: "Sentence was successfully created." }
format.json { render :show, status: :created, location: @sentence }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @sentence.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /sentences/1 or /sentences/1.json
def update
respond_to do |format|
if @sentence.update(sentence_params)
format.html { redirect_to sentence_url(@sentence), notice: "Sentence was successfully updated." }
format.json { render :show, status: :ok, location: @sentence }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @sentence.errors, status: :unprocessable_entity }
end
end
end
# DELETE /sentences/1 or /sentences/1.json
def destroy
@sentence.destroy
respond_to do |format|
format.html { redirect_to sentences_url, notice: "Sentence was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_sentence
@sentence = Sentence.find(params[:id])
end
# Only allow a list of trusted parameters through.
def sentence_params
params.require(:sentence).permit(:body, :account_id)
end
end
module SentencesHelper
end
class Sentence < ApplicationRecord
belongs_to :account
end
<%= form_with(model: sentence) do |form| %>
<% if sentence.errors.any? %>
<div style="color: red">
<h2><%= pluralize(sentence.errors.count, "error") %> prohibited this sentence from being saved:</h2>
<ul>
<% sentence.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :body, style: "display: block" %>
<%= form.text_area :body %>
</div>
<div>
<%= form.label :account_id, style: "display: block" %>
<%= form.text_field :account_id %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
<div id="<%= dom_id sentence %>">
<p>
<strong>Body:</strong>
<%= sentence.body %>
</p>
<p>
<strong>Account:</strong>
<%= sentence.account_id %>
</p>
</div>
json.extract! sentence, :id, :body, :account_id, :created_at, :updated_at
json.url sentence_url(sentence, format: :json)
<h1>Editing sentence</h1>
<%= render "form", sentence: @sentence %>
<br>
<div>
<%= link_to "Show this sentence", @sentence %> |
<%= link_to "Back to sentences", sentences_path %>
</div>
<p style="color: green"><%= notice %></p>
<h1>Sentences</h1>
<div id="sentences">
<% @sentences.each do |sentence| %>
<%= render sentence %>
<p>
<%= link_to "Show this sentence", sentence %>
</p>
<% end %>
</div>
<%= link_to "New sentence", new_sentence_path %>
json.array! @sentences, partial: "sentences/sentence", as: :sentence
<h1>New sentence</h1>
<%= render "form", sentence: @sentence %>
<br>
<div>
<%= link_to "Back to sentences", sentences_path %>
</div>
<p style="color: green"><%= notice %></p>
<%= render @sentence %>
<div>
<%= link_to "Edit this sentence", edit_sentence_path(@sentence) %> |
<%= link_to "Back to sentences", sentences_path %>
<%= button_to "Destroy this sentence", @sentence, method: :delete %>
</div>
json.partial! "sentences/sentence", sentence: @sentence
Rails.application.routes.draw do
resources :sentences
resources :accounts
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
......
class CreateSentences < ActiveRecord::Migration[7.0]
def change
create_table :sentences do |t|
t.text :body
t.references :account, null: false, foreign_key: true
t.timestamps
end
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_04_25_032546) do
ActiveRecord::Schema[7.0].define(version: 2022_04_25_113435) do
create_table "accounts", force: :cascade do |t|
t.string "email"
t.string "password_digest"
......@@ -19,4 +19,13 @@ ActiveRecord::Schema[7.0].define(version: 2022_04_25_032546) do
t.index ["email"], name: "index_accounts_on_email", unique: true
end
create_table "sentences", force: :cascade do |t|
t.text "body"
t.integer "account_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["account_id"], name: "index_sentences_on_account_id"
end
add_foreign_key "sentences", "accounts"
end
require "test_helper"
class SentencesControllerTest < ActionDispatch::IntegrationTest
setup do
@sentence = sentences(:one)
end
test "should get index" do
get sentences_url
assert_response :success
end
test "should get new" do
get new_sentence_url
assert_response :success
end
test "should create sentence" do
assert_difference("Sentence.count") do
post sentences_url, params: { sentence: { account_id: @sentence.account_id, body: @sentence.body } }
end
assert_redirected_to sentence_url(Sentence.last)
end
test "should show sentence" do
get sentence_url(@sentence)
assert_response :success
end
test "should get edit" do
get edit_sentence_url(@sentence)
assert_response :success
end
test "should update sentence" do
patch sentence_url(@sentence), params: { sentence: { account_id: @sentence.account_id, body: @sentence.body } }
assert_redirected_to sentence_url(@sentence)
end
test "should destroy sentence" do
assert_difference("Sentence.count", -1) do
delete sentence_url(@sentence)
end
assert_redirected_to sentences_url
end
end
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
body: MyText
account: one
two:
body: MyText
account: two
require "test_helper"
class SentenceTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
require "application_system_test_case"
class SentencesTest < ApplicationSystemTestCase
setup do
@sentence = sentences(:one)
end
test "visiting the index" do
visit sentences_url
assert_selector "h1", text: "Sentences"
end
test "should create sentence" do
visit sentences_url
click_on "New sentence"
fill_in "Account", with: @sentence.account_id
fill_in "Body", with: @sentence.body
click_on "Create Sentence"
assert_text "Sentence was successfully created"
click_on "Back"
end
test "should update Sentence" do
visit sentence_url(@sentence)
click_on "Edit this sentence", match: :first
fill_in "Account", with: @sentence.account_id
fill_in "Body", with: @sentence.body
click_on "Update Sentence"
assert_text "Sentence was successfully updated"
click_on "Back"
end
test "should destroy Sentence" do
visit sentence_url(@sentence)
click_on "Destroy this sentence", match: :first
assert_text "Sentence was successfully destroyed"
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment