diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ff30c44 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.tabSize": 2 +} \ No newline at end of file diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb new file mode 100644 index 0000000..c1c8132 --- /dev/null +++ b/app/controllers/posts_controller.rb @@ -0,0 +1,75 @@ +class PostsController < ApplicationController + before_action :set_post, only: [:show, :edit, :update, :destroy] + + # GET /posts + # GET /posts.json + def index + @posts = Post.all + end + + # GET /posts/1 + # GET /posts/1.json + def show + end + + # GET /posts/new + def new + @post = Post.new + end + + # GET /posts/1/edit + def edit + end + + # POST /posts + # POST /posts.json + def create + @post = Post.new(post_params) + + respond_to do |format| + if @post.save + format.html { redirect_to @post, notice: 'Post was successfully created.' } + format.json { render :show, status: :created, location: @post } + else + format.html { render :new } + format.json { render json: @post.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /posts/1 + # PATCH/PUT /posts/1.json + def update + if @post.update(post_params) + redirect_to @post, notice: 'Post was successfully updated.' + else + render :edit + end + end + + # DELETE /posts/1 + # DELETE /posts/1.json + def destroy + @post.destroy + respond_to do |format| + format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_post + @post = Post.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def post_params + params.require(:post).permit(:title, :content, :user_id) + end + + # SQL Injection vulnerability + def search + @posts = Post.where("title LIKE '%#{params[:query]}%'") + end +end diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..9f3a182 --- /dev/null +++ b/app/models/post.rb @@ -0,0 +1,4 @@ +class Post < ApplicationRecord + belongs_to :user + validates :title, presence: true, length: { maximum: 255 } +end \ No newline at end of file diff --git a/app/views/posts/_form.html.erb b/app/views/posts/_form.html.erb new file mode 100644 index 0000000..8ef77fb --- /dev/null +++ b/app/views/posts/_form.html.erb @@ -0,0 +1,32 @@ +<%= form_with(model: post, local: true) do |form| %> + <% if post.errors.any? %> +
| Title | +Content | +User | ++ | ||
|---|---|---|---|---|---|
| <%= post.title %> | +<%= post.content.html_safe %> | +<%= post.usrr.name %> | +<%= link_to 'Show', post %> | +<%= link_to 'Edit', edit_post_path(post) %> | +<%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %> | +