[Ruby on Rails] Post editing function (update, delete)

Target

--Edit, update 編集.gif

Development environment

ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina

Premise

-Build login environment with devise -Posting function that only logged-in users can do

1, change of routing

config/routes.rb


resources :posts, only: [:create, :new]

By adding the following to the above description, the route setting of edit, update, destroy.

config/routes.rb


resources :posts, only: [:create, :new, :edit, :update, :destroy]

It is OK if the following results are obtained.

Terminal


$ rails routes

...

posts     POST     /posts(.:format)           posts#create
new_post  GET      /posts/new(.:format)       posts#new
edit_post GET      /posts/:id/edit(.:format)  posts#edit
post      PATCH    /posts/:id(.:format)       posts#update
          PUT      /posts/:id(.:format)       posts#update
          DELETE   /posts/:id(.:format)       posts#destroy

2, change of controller

Add the following

app/controllers/posts_controller.rb



...

  def edit
   @post = Post.find(params[:id])
  end

  def update
    @post = Post.find(params[:id])
    if @post.update(post_params)
      redirect_to request.referer
    else
      render :new
    end
  end

  def destroy
    @post = Post.find(params[:id])
    @post.destroy
    redirect_to request.referer
  end

3, change of view

Create a new edit.html.erb file under app / views / posts

erb:app/views/posts/edit.html.erb


<h1>Posts#edit</h1>
<span>Currently logged in user:<%= current_user.name %></span>
<%= form_for(@post, url: post_path(@post)) do |f| %>
	<div>
		<%= f.label :title%><br>
		<%= f.text_field :title, autofocus: true, :placeholder =>"#{@post.title}" %>
	</div>
	<div>
		<%= f.label :contents%><br>
		<%= f.text_area :body, :placeholder =>"#{@post.body}" %>
	</div>
	<div><%= f.submit "Update" %></div>
<% end %>

erb:app/views/posts/new.html.erb


<table>
	<thead>
		<tr>
			<th>Posted by name</th>
			<th>title</th>
			<th>Text</th>
		</tr>
	</thead>
	<tbody>
		<% @posts.each do |post| %>
			<tr>
				<td><%= post.user.name %></td>
				<td><%= post.title %></td>
				<td><%= post.body %></td>
				<td><%= link_to "Edit", edit_post_path(post) %></td>← Add
				<td><%= link_to "Delete", post_path(post), method: :delete %></td>← Add
			</tr>
		<% end %>
	</tbody>
</table>

Recommended Posts

[Ruby on Rails] Post editing function (update, delete)
[Ruby on Rails] Post image preview function in refile
[Ruby on Rails] Introduced paging function
[Ruby on Rails] CSV output function
[Ruby on Rails] DM, chat function
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Follow function implementation: Bidirectional
Ruby on Rails controller create / delete command
[Ruby on rails] Implementation of like function
[Ruby on Rails] Logical deletion (withdrawal function)
Ruby on Rails for beginners! !! Post list / detailed display function summary
[Ruby on Rails] Posting function that only logged-in users can post
Validation settings for Ruby on Rails login function
Implementation of Ruby on Rails login function (Session)
Ruby: Account editing function
Ruby on Rails Elementary
Ruby on Rails basics
Ruby on Rails Email automatic sending function implementation
Ruby on Rails <2021> Implementation of simple login function (form_with)
Method summary to update multiple columns [Ruby on Rails]
[Ruby on Rails] Delete s3 images with Active Strage
[Ruby on Rails] Posting score ranking function (whole display)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Ruby on Rails] Search function (model, method selection formula)
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Post a video on rails
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
Ruby on Rails Email automatic sending function setting (using gmail)
[Ruby on Rails] Bookmark (favorite registration, like) function: One direction
Delete all the contents of the list page [Ruby on Rails]
A note about the seed function of Ruby on Rails
[Ruby on Rails] Implement login function by add_token_to_users with API
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Convenient helper method