22
loading...
This website collects cookies to deliver better user experience
$ rails g scaffold Post
create db/migrate/20211010101809_create_posts.rb
create app/models/post.rb
invoke resource_route
route resources :posts
invoke scaffold_controller
create app/controllers/posts_controller.rb
create app/views/posts
create app/views/posts/index.html.erb
create app/views/posts/edit.html.erb
create app/views/posts/show.html.erb
create app/views/posts/new.html.erb
create app/views/posts/_form.html.erb
class CreatePosts < ActiveRecord::Migration[6.1]
def change
create_table :posts do |t|
t.string :title
t.text :text
t.timestamps
end
end
end
$ rails db:migrate
== 20211010101809 CreatePosts: migrating ======================================
-- create_table(:posts)
-> 0.0032s
== 20211010101809 CreatePosts: migrated (0.0040s) =============================
<%= form_with(model: post) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% post.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form.label :title, "Title:" %>
<%= form.text_field :title %>
<%= form.label :text, "Text:" %>
<%= form.text_area :text %>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
class PostsController < ApplicationController
#---------
# Other actions
#---------
def post_params
params.require(:post).permit(:title, :text)
end
end
<p id="notice"><%= notice %></p>
<h1>Posts</h1>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Post', new_post_path %>
<! -- app/views/posts/show.html.erb -- >
<p id="notice"><%= notice %></p>
<%= @post.title %>
<br>
<%= @post.text %>
<br>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
$ rails g scaffold Comment
create db/migrate/20211017054528_create_comments.rb
create app/models/comment.rb
invoke scaffold_controller
create app/controllers/comments_controller.rb
invoke erb
create app/views/comments
create app/views/comments/index.html.erb
create app/views/comments/edit.html.erb
create app/views/comments/show.html.erb
create app/views/comments/new.html.erb
create app/views/comments/_form.html.erb
class CreateComments < ActiveRecord::Migration[6.1]
def change
create_table :comments do |t|
t.integer :post_id
t.text :text
t.timestamps
end
end
end
$ rails db:migrate
== 20211017054528 CreateComments: migrating ===================================
-- create_table(:comments)
-> 0.0215s
== 20211017054528 CreateComments: migrated (0.0218s) ==========================
#models/post.rb
class Post < ApplicationRecord
has_many :comments
end
#models/comment.rb
class Post < ApplicationRecord
belongs_to :post
end
<%= form_with(model: comment) do |form| %>
<% if comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(comment.errors.count, "error") %> prohibited this comment from being saved:
</h2>
<ul>
<% comment.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form.hidden_field :post_id %>
<%= form.label :text, "Text:" %>
<%= form.text_area :text %>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
class CommentsController < ApplicationController
#---------
# Other actions
#---------
def comment_params
params.require(:comment).permit(:post_id, :text)
end
end
<p id="notice"><%= notice %></p>
<%= @post.title %>
<br>
<%= @post.text %>
<br>
<%= render "comments/form", comment: @comment%>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
@comment
variable used for the comment form.class PostsController < ApplicationController
#---------
# Other actions
#---------
def show
@comment = @post.comments.build
end
#---------
# Other actions
#---------
end
@comment
each time you show a post. The comment form under the post should work now but if the form has been submitted, the page redirects to "localhost:3000/comments/[id]" instead of "localhost:3000/posts/[id]".class CommentsController < ApplicationController
#---------
# Other actions
#---------
def create
@comment = Comment.new(comment_params)
respond_to do |format|
if @comment.save
format.html { redirect_to @comment.post, notice: "Comment was successfully created." }
else
format.html { render :new, status: :unprocessable_entity }
end
end
end
#---------
# Other actions
#---------
end
<p id="notice"><%= notice %></p>
<%= @post.title %>
<br>
<%= @post.text %>
<br>
<b>Comments</b>
<br>
<%- @post.comments.each do |comment|%>
<%= comment.text%>
<br>
<% end %>
<%= render "comments/form", comment: @comment%>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>