Home>

We have created a site similar to twitter with rails.
So I would like to move from show.html.erb to edit.html.erb to finish post editing.
Somehow, it will move to confirm.html.erb when posting new.

post_controller.rb
class PostsController<ApplicationController
  def index
    @posts = Post.all.order (created_at:: desc)
  end
  def show
    @post = Post.find_by (id: params [: id])
  end
  def new
    @post = Post.new
  end
  def create
    @post = Post.new (content: params [: content])
    if params [: back]
      render: new
      return
    end
    unless @ post.valid?
      render: new
      return
    end
    if params [: send]
      @ post.save!
      flash [: notice] = "Post created"
      redirect_to '/ posts # index'
      return
    end
    render: confirm
  end
  def edit
    @post = Post.find_by (id: params [: id])
  end
  def update
    @post = Post.find_by (id: params [: id])
    @ post.content = params [: content]
    if @ post.save
      flash [: notice] = "Post edited"
      redirect_to "/ posts # index"
    else
      render "posts # edit"
    end
  end

  def destroy
    @post = Post.find_by (id: params [: id])
    @ post.destroy
    flash [: notice] = "Post deleted"
    redirect_to "/ posts # index"
  end
  def confirm
    @post = Post.new (params [: post])
    if @ post.invalid?
     render: action =>"posts # confirm"
    else
     render: action =>"posts # index"
    end
  end

end
show.html.erb (screen for displaying posts)
<div>
  <div>
    <div>
      <p>
        <% = @ post.content%>
      </p>
      <div>
        <% = @ post.created_at%>
      </div>
      <div>
        <% = link_to ("edit", "/posts/#{@post.id}/edit")%>
        <% = link_to ("Delete", "/posts/#{@post.id}/destroy", {method: "post"})%>
      </div>
    </div>
  </div>
</div>
edit.html.erb (post edit screen)
<div>
  <div>
    <h1>Edit</h1>
    <% = form_tag ("/ posts # update") do%>
      <div>
        <div>
          <% @ post.errors.full_messages.each do | message |%>
            <div>
              <% = message%>
            </div>
          <% end%>
          <textarea name = "content"><% = @ post.content%></textarea>
          <input type = "submit" value = "Save">
        </div>
      </div>
    <% end%>
  </div>
</div>
confirm.html.erb (post confirmation screen)
<div>
  <div>
  <h1>Check your post</h1>
  <% = form_with (model: @post, url: posts_path, local: true) do | form |%>
  <div>
    <div>
    <div>
      <% = @ post.content%>
      <% = hidden_field_tag: content, @ post.content%>
    </div>
    <div>
      <% = submit_tag 'Submit', name: 'send', class: "btn btn-link"%>
    </div>
    <div>
      <% = submit_tag 'back', name: 'back', class: "btn btn-link"%>
    </div>
    </div>
    </div>
<% end%>
</div>
</did>
routes.rb
Rails.application.routes.draw do
  root "home # top"
  resources: sessions, only: [: new,: create,: destroy]
  resources: users
  resources: posts do
    collection do
      post: confirm
    end
  end
  get "about", to: "home # about"
end


  • Answer # 1

    I think the cause is theform_tag ("/posts # update") dopart of

    edit.html.erb.
    Tryform_tag ('/posts&apos ;, method:: patch).
    http://railsdoc.com/references/form_tag

    Or just like confirm.html.erb,form_with (model: @post, local: true)may be used.

Related articles