(Ruby on Rails6) Create a function to edit the posted content

Preface

Here, the work of "editing the posted content" is left as an oblivion record on the assumption that the post page has been created.

Create a function to edit the posted content

Create edit page

Please create a new edit page at 3 points of routes, controllers, and view. Also, for page creation

・ Controller name → form ・ Action name → Edit

It is set with.

(Example)

config/routes


Rails.application.routes.draw do
  get '/' => 'form#index'
  get '/form' => 'form#form'
  post "form/create" => "form#create"
  get "form/:id/edit" => "form#edit"⇦ This
  get "form/:id" => "form#post"
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

erb:app/views/Any.html.erb


<h1>Edit page</h1>

rb:app/controllers/Any.controller.rb


class FormController < ApplicationController
  def index
    @forms = Form.all
    @forms = Form.all.order(created_at: :desc)
  end
  def post
    @forms = Form.find_by(id: params[:id])
  end
  def form
    @forms = Form.find_by(id: params[:id])
  end
  def create
    @forms = Form.new(content: params[:content])
    @forms.save 
    @forms = Form.new(title: params[:title])
    @forms.save 
    redirect_to("/")
  end

def edit ← from here
   
end ← up to here
end

The post </ strong> of routes is used when retrieving form data. Please set with get </ strong> when creating a new page. It will cause an error.

Initial value setting on the edit page (textarea / input)

controllers

rb:app/views/Any.html.erb


・
・
・
def edit
    @forms = Form.find_by(id: params[:id])← here
end

Set @forms = Form.find_by (id: params [: id]) </ strong> in the edit action to get the data with the same id as the URL.

views

erb:app/views/Any.html.erb


<h1>Edit page</h1>
<div class="form">
  <div>
    <textarea><%= @forms.content %></textarea>← here
    <input type="submit" value="Save"> </div>
</div>

<% = @ forms.content%> </ strong> sets the initial value of the input.

Save edits

Add action to controller

You will receive the value from the form, so in routes you should do it with post </ strong> instead of get.

config/routes.rb


post "form/:id/new" => "form#new"

Redirect settings

When you're ready to receive a value from a form for an action, you need to redirect </ strong> after pressing submit.

rb:app/views/Any.controllers.rb


def new
    redirect_to("/")
end

Use redirect_to ("/") </ strong> to set the redirect. / </ strong> is set by me, so please arrange it individually.

Specify the destination in Views

I got the value from the form and redirected it earlier. Here, specify the destination in Views.

erb:app/views/Any.controllers.erb


<h1>Edit page</h1>
<%= form_tag("/posts/#{@post.id}/new") do %>← here
<div class="form">
  <div>
    <textarea><%= @forms.title %></textarea> 
    <textarea><%= @forms.content %></textarea> 
    <input type="submit" value="Save"> </div>
</div>
<% end %>← here

↓ Details

erb:app/views/Any.html.erb


<%= form_tag("/posts/#{@post.id}/new") do %>
Form tag
<% end %>

The destination is specified by ↑.

Update the posted content

I need to get the data from the database. The acquisition action can be performed by adding the action name attribute, so let's execute it.

Specify name attribute in View

erb:app/views/Any.html.erb


<h1>Edit page</h1>
<%= form_tag("/posts/#{@post.id}/new") do %>
<div class="form">
  <div>
    <textarea name="content"><%= @forms.content %></textarea>← here
    <input type="submit" value="Save"> </div>
</div>
<% end %>

Specified by params and save on the controller

params </ strong> allows you to create an object to hold the data sent.

rb:app/controllers/Any.controller.rb


def update
    @forms = Form.find_by(id: params[:id])
    @forms.content = params[:content]
    @forms.save
    
    redirect_to("/")
  end

Afterword

Thank you for reading this far. It may take some time before you can understand the process such as setting the initial value, but I would like to steadily deepen your understanding. Also, since the details such as "link to the posting page" are omitted from the introduction here, it is recommended that you set the one you make yourself.

Reference link

<a href="https://www.amazon.co.jp/Ruby-Rails-%E5%AE%9F%E8%B7%B5%E3%82%AC%E3%82%A4%E3%83% 89-impress-gear / dp / 4295008052 "target =" blank "> Ruby on Rails6 Practical Guide

My link

Also, there is a link on Twitter / Portfolio, so if you are interested, Please connect. I would be very happy to have friends who can share programming learning.

Twitter Portfolio Github

Recommended Posts