create micropost

parent 03273a5e
Pipeline #1202 canceled with stages
in 0 seconds
// Place all the styles related to the Microposts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
class MicropostsController < ApplicationController
before_action :set_micropost, only: [:show, :edit, :update, :destroy]
# GET /microposts
# GET /microposts.json
def index
@microposts = Micropost.all
end
# GET /microposts/1
# GET /microposts/1.json
def show
end
# GET /microposts/new
def new
@micropost = Micropost.new
end
# GET /microposts/1/edit
def edit
end
# POST /microposts
# POST /microposts.json
def create
@micropost = Micropost.new(micropost_params)
respond_to do |format|
if @micropost.save
format.html { redirect_to @micropost, notice: 'Micropost was successfully created.' }
format.json { render :show, status: :created, location: @micropost }
else
format.html { render :new }
format.json { render json: @micropost.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /microposts/1
# PATCH/PUT /microposts/1.json
def update
respond_to do |format|
if @micropost.update(micropost_params)
format.html { redirect_to @micropost, notice: 'Micropost was successfully updated.' }
format.json { render :show, status: :ok, location: @micropost }
else
format.html { render :edit }
format.json { render json: @micropost.errors, status: :unprocessable_entity }
end
end
end
# DELETE /microposts/1
# DELETE /microposts/1.json
def destroy
@micropost.destroy
respond_to do |format|
format.html { redirect_to microposts_url, notice: 'Micropost was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_micropost
@micropost = Micropost.find(params[:id])
end
# Only allow a list of trusted parameters through.
def micropost_params
params.require(:micropost).permit(:content, :user_id)
end
end
module MicropostsHelper
end
class Micropost < ApplicationRecord
end
<%= form_with(model: micropost) do |form| %>
<% if micropost.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(micropost.errors.count, "error") %> prohibited this micropost from being saved:</h2>
<ul>
<% micropost.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :content %>
<%= form.text_area :content %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
json.extract! micropost, :id, :content, :user_id, :created_at, :updated_at
json.url micropost_url(micropost, format: :json)
<h1>Editing Micropost</h1>
<%= render 'form', micropost: @micropost %>
<%= link_to 'Show', @micropost %> |
<%= link_to 'Back', microposts_path %>
<p id="notice"><%= notice %></p>
<h1>Microposts</h1>
<table>
<thead>
<tr>
<th>Content</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @microposts.each do |micropost| %>
<tr>
<td><%= micropost.content %></td>
<td><%= micropost.user_id %></td>
<td><%= link_to 'Show', micropost %></td>
<td><%= link_to 'Edit', edit_micropost_path(micropost) %></td>
<td><%= link_to 'Destroy', micropost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Micropost', new_micropost_path %>
json.array! @microposts, partial: "microposts/micropost", as: :micropost
<h1>New Micropost</h1>
<%= render 'form', micropost: @micropost %>
<%= link_to 'Back', microposts_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Content:</strong>
<%= @micropost.content %>
</p>
<p>
<strong>User:</strong>
<%= @micropost.user_id %>
</p>
<%= link_to 'Edit', edit_micropost_path(@micropost) %> |
<%= link_to 'Back', microposts_path %>
json.partial! "microposts/micropost", micropost: @micropost
<p id="notice"><%= notice %></p> <p id="notice"><%= notice %></p>
<h1>Users</h1> <h1>Users</h1>
<table> <table>
<thead> <thead>
<tr> <tr>
<th>Name</th> <th>Name</th>
<th>Email</th> <th>Email</th>
<th colspan="3"></th> <th colspan="3"></th>
</tr> </tr>
</thead> </thead>
<tbody>
<tbody> <% @users.each do |user| %>
<% @users.each do |user| %> <tr>
<tr> <td><%= user.name %></td>
<td><%= user.name %></td> <td><%= user.email %></td>
<td><%= user.email %></td> <td><%= link_to 'Show', user %></td>
<td><%= link_to 'Show', user %></td> <td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td> <td><%= link_to 'Destroy', user, method: :delete,
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td> data: { confirm: 'Are you sure?' } %></td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
<br> <br>
<%= link_to 'New User', new_user_path %>
<%= link_to 'New User', new_user_path %> \ No newline at end of file
...@@ -28,7 +28,7 @@ Rails.application.configure do ...@@ -28,7 +28,7 @@ Rails.application.configure do
# config.assets.css_compressor = :sass # config.assets.css_compressor = :sass
# Do not fallback to assets pipeline if a precompiled asset is missed. # Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true config.assets.compile = false
# Enable serving of images, stylesheets, and JavaScripts from an asset server. # Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.asset_host = 'http://assets.example.com' # config.asset_host = 'http://assets.example.com'
......
Rails.application.routes.draw do Rails.application.routes.draw do
resources :microposts
resources :users resources :users
root 'application#hello' root 'users#index'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end end
class CreateMicroposts < ActiveRecord::Migration[6.1]
def change
create_table :microposts do |t|
t.text :content
t.integer :user_id
t.timestamps
end
end
end
...@@ -10,7 +10,14 @@ ...@@ -10,7 +10,14 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_06_01_082234) do ActiveRecord::Schema.define(version: 2021_06_01_155321) do
create_table "microposts", force: :cascade do |t|
t.text "content"
t.integer "user_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|
t.string "name" t.string "name"
......
require "test_helper"
class MicropostsControllerTest < ActionDispatch::IntegrationTest
setup do
@micropost = microposts(:one)
end
test "should get index" do
get microposts_url
assert_response :success
end
test "should get new" do
get new_micropost_url
assert_response :success
end
test "should create micropost" do
assert_difference('Micropost.count') do
post microposts_url, params: { micropost: { content: @micropost.content, user_id: @micropost.user_id } }
end
assert_redirected_to micropost_url(Micropost.last)
end
test "should show micropost" do
get micropost_url(@micropost)
assert_response :success
end
test "should get edit" do
get edit_micropost_url(@micropost)
assert_response :success
end
test "should update micropost" do
patch micropost_url(@micropost), params: { micropost: { content: @micropost.content, user_id: @micropost.user_id } }
assert_redirected_to micropost_url(@micropost)
end
test "should destroy micropost" do
assert_difference('Micropost.count', -1) do
delete micropost_url(@micropost)
end
assert_redirected_to microposts_url
end
end
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
content: MyText
user_id: 1
two:
content: MyText
user_id: 1
require "test_helper"
class MicropostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
require "application_system_test_case"
class MicropostsTest < ApplicationSystemTestCase
setup do
@micropost = microposts(:one)
end
test "visiting the index" do
visit microposts_url
assert_selector "h1", text: "Microposts"
end
test "creating a Micropost" do
visit microposts_url
click_on "New Micropost"
fill_in "Content", with: @micropost.content
fill_in "User", with: @micropost.user_id
click_on "Create Micropost"
assert_text "Micropost was successfully created"
click_on "Back"
end
test "updating a Micropost" do
visit microposts_url
click_on "Edit", match: :first
fill_in "Content", with: @micropost.content
fill_in "User", with: @micropost.user_id
click_on "Update Micropost"
assert_text "Micropost was successfully updated"
click_on "Back"
end
test "destroying a Micropost" do
visit microposts_url
page.accept_confirm do
click_on "Destroy", match: :first
end
assert_text "Micropost was successfully destroyed"
end
end
Markdown is supported
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