Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Routes:
- Rails.application.routes.draw do
- root "posts#index"
- resources :posts do
- resources :comments
- end
- end
- Post Controller:
- class PostsController < ApplicationController
- def index
- @posts = Post.all
- if @posts.blank?
- flash[:alert] = "No posts have been created."
- else
- @posts
- end
- end
- def edit
- @post = Post.find(params[:id])
- end
- def update
- @post = Post.find(params[:id])
- if @post.update(post_params)
- flash[:notice] = "Post has been updated."
- redirect_to @post
- else
- flash.now[:alert] = "Post has not been updated."
- render "edit"
- end
- end
- def create
- @post = Post.new(post_params)
- if @post.save
- flash[:notice] = "Post has been created."
- redirect_to @post
- else
- flash[:notice] = "Post has not been created."
- end
- end
- def new
- @post = Post.new
- end
- def show
- @post = Post.find(params[:id])
- @comment = @post.comments.find(params[:id])
- end
- def destroy
- @post = Post.find(params[:id])
- @post.destroy
- flash[:notice] = "Post has been deleted."
- redirect_to posts_path
- end
- private
- def post_params
- params.require(:post).permit(:title, :author, :body)
- end
- end
- Comments Controller:
- class CommentsController < ApplicationController
- def create
- @post = Post.find(params[:post_id])
- @comment = @post.comments.create(comment_params)
- redirect_to post_path(@post)
- end
- private
- def comment_params
- params.require(:comment).permit(:author, :body)
- end
- end
- Comments View Files
- _comment.html.erb
- <p>
- Author:
- <%= comment.author %>
- </p>
- <p>
- Body:
- <%= comment.body %>
- </p>
- _form.html.erb
- <%= form_for([@post, @post.comments.build]) do |f| %>
- <p>
- <%= f.label :author %><br />
- <%= f.text_field :author %>
- </p>
- <p>
- <%= f.label :body %><br />
- <%= f.text_area :body %>
- </p>
- <p>
- <%= f.submit %>
- </p>
- <% end %>
- Post View Files
- _form.html.erb
- <%= form_for @post do |f| %>
- <p>
- <%= f.label :title %><br />
- <%= f.text_field :title %>
- </p>
- <p>
- <%= f.label :author %><br />
- <%= f.text_field :author %>
- </p>
- <p>
- edit.html.erb
- <h3>Edit Post</h3>
- <%= render 'form' %>
- index.html.erb
- <h1>Overall Posts</h1>
- <% @posts.each do |post| %>
- <p>
- <b>Title:</b>
- <%= post.title %>
- </p>
- <p>
- <b>Author:</b>
- <%= post.author %>
- </p>
- <p>
- <b>Body</b>
- <%= post.body %>
- </p>
- <p>
- <%= link_to 'Show', post_path(post) %> |
- <%= link_to 'Edit', edit_post_path(post) %> |
- <%= link_to 'Destroy', post_path(post), method: :delete, data: { confirm: 'Are you sure?' } %>
- </p>
- <% end %>
- <p><%= link_to "Create New Post", new_post_path %>
- new.html.erb
- <h3>Create New Post</h3>
- <%= render 'form' %>
- <p><%= link_to "Posts Index", posts_path %></p>
- show.html.erb
- <p><%= link_to "Posts Index", posts_path %></p>
- <h3>Post's details</h3>
- <h3>Title</h3>
- <%= @post.title %>
- <h3>Author</h3>
- <%= @post.author %>
- <h3>Body</h3>
- <%= @post.body %>
- <h2>Recent Comments</h2>
- <%= render @comments.comments %>
- <h2>Add Comments</h2>
- <%= render 'comments/form' %>
- <%= f.label :body %><br />
- <%= f.text_area :body %>
- </p>
- <p>
- <%= f.submit %>
- </p>
- <% end %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement