前提・実現したいこと
railsで簡単なtwitterに似たサイトを作成しております。
そこでshow.html.erbからedit.html.erbに移動して投稿編集を終わらせたいのですが、
なぜか新規で投稿をするときのconfirm.html.erbに移動してしまいます。
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] = "投稿を作成しました" 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] = "投稿を編集しました" redirect_to "/posts#index" else render "posts#edit" end end def destroy @post = Post.find_by(id: params[:id]) @post.destroy flash[:notice] = "投稿を削除しました" 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(投稿を表示する画面)
<div class="main posts-show"> <div class="container"> <div class="posts-show-item"> <p> <%= @post.content %> </p> <div class="post-time"> <%= @post.created_at %> </div> <div class="post-menus"> <%= link_to("編集", "/posts/#{@post.id}/edit") %> <%= link_to("削除", "/posts/#{@post.id}/destroy", {method: "post"}) %> </div> </div> </div> </div>
edit.html.erb(投稿編集画面)
<div class="main posts-new"> <div class="container"> <h1 class="form-heading">編集する</h1> <%= form_tag("/posts#update") do %> <div class="form"> <div class="form-body"> <% @post.errors.full_messages.each do |message| %> <div class="form-error"> <%= message %> </div> <% end %> <textarea name="content"><%= @post.content %></textarea> <input type="submit" value="保存"> </div> </div> <% end %> </div> </div>
confirm.html.erb(投稿確認画面)
<div class="main posts-new"> <div class="container"> <h1 class="form-heading">投稿内容を確認してください</h1> <%= form_with(model: @post, url: posts_path, local: true) do |form| %> <div class="form"> <div class="form-body"> <div class="field"> <%= @post.content %> <%= hidden_field_tag :content, @post.content %> </div> <div class="actions"> <%= submit_tag '投稿する', name: 'send',class: "btn btn-link"%> </div> <div class="actions"> <%= submit_tag '戻る', 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
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/28 05:42