简体   繁体   中英

How to convert from <%= form_for @post do |f| %> to form_with

Building my first rails app with a built-in blog using form_for but noticed that it's now been replaced with form_with...

I need to update to the new format as I would like to add TinyMCE or action_text so I can add images, style blog posts ect and if possible include prismjs so I can use highlighted syntax in the blogs.

I've tried bin/rails action_text:install and going can get that to work, but I think that's the wrong way to do I t as I have already for the posts_controller.rb setup.

Any feedback or direction would be amazing..

Current _form.html.erb

<%= form_for @post do |f| %>
    <%= f.label :title %><br>
    <%= f.text_field :title %><br>
    <%= f.label :body %><br>
    <%= f.text_area :body %><br>
    <%= f.submit %><br>
<%end%>

Current posts_controller.rb

class PostsController < ApplicationController
  before_action :find_post, only: [:show, :edit, :update, :destroy] #// before actions route/run it will find the actions identified
  before_action :authenticate_user!, except: [:index, :show]
  def index
    @posts = Post.all.order("created_at DESC")
  end
  def new
    @post = Post.new
  end
#// Create

  def create
    @post = Post.new(post_params)
    if @post.save
      redirect_to @post
    else
      render "new"
    end
  end

  #// Read
  
  def show
  end
  
  def edit
  end

  #// Update
  def update
    if @post.update(post_params)
      redirect_to @post
    else
      render 'edit'
    end
  end

  #// Destroy

  def destroy
    @post.destroy
    redirect_to root_path
  end


  private

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

  def post_params
    params.require(:post).permit(:title, :body)
  end
end

Current show.html.erb

<div class="card text-center my-3">
  <div class="card-header">
    Featured
  </div>
  <div class="card-body">
    <h5 class="card-title"><%= @post.title %></h5>
    <p class="card-text"><%= @post.body%></p>
   
    <p> <small>Author: KKS</small></p>
    <p><small>Created: <%= time_ago_in_words(@post.created_at) %> ago<small></p>

    <button class="btn btn-primary btn-sm"><%= link_to 'Back', posts_path, class:"nav-link"%></button>
  </div>
  <div class="card-footer text-muted">
    <p><small>Updated: <%= time_ago_in_words(@post.updated_at) %> ago</small></p>
        <% if user_signed_in? %> 
        <%= link_to "Edit", edit_post_path(@post) %>
        <%= button_to "Delete", post_path(@post), method: :delete, data: { confirm: "Are you sure you want to delete?"} %>
    <% end %>
  </div>
</div>

Current index.html.erb

<div class="container my-3 d-grid gap-3 d-flex flex-wrap flex-direction-row justify-content-center align-items-stretch aligh-content-start">
    <% @posts.each do |post|%>
        <div class="card flex-fill mx-3 my-3" style="width: 18rem;"> 
            <img src="/assets/blog_sample_img.png" class="card-img-top" alt="...">
            <div class="card-body">
                <h5 class="card-title"><%= post.title %></h5>
                <div><%= truncate(post.body, :length => 300)%></div>
                <br>
                <button class="btn btn-primary btn-sm"><%= link_to "Read Post", post %></button>
            </div>
        </div>
    <%end%>
</div>

To convert your form_for into form_with just adapt syntax and don't forget that form_with is remote by default:

form_for @post do |f|

become:

form_with(model: @post, local:true) do |f|

Look Medium article

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM