前提・実現したいこと
Railsを使用してtwitterのクローンのようなサイトを作成しております。
そこでtitterでいうツイートをつぶやいた時に入力画面 => 確認画面 => 入力一覧画面のように作りたいのですが、
確認画面が表示されず、すぐに入力一覧画面が表示されてしまいます。
入力画面 => 確認画面 => 入力一覧画面と表示する方法を教えていただけますでしょうか?
発生している問題・エラーメッセージ
入力画面(new.html.erb)=>確認画面(confirm.html.erb)=>入力一覧画面(index.html.erb)
という順番に表示したい。
該当のソースコード
posts_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
render :action => "new"
end
def create
@post = Post.new(content: params[:content])
if params[:back]
render("posts/new")
elsif @post.save
flash[:notice] = "投稿を作成しました"
redirect_to("/posts/index")
else
render("posts/new")
end
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[:content])
if @post.invalid?
render :action => "confirm"
else
render :action => "new"
end
end
end
app/views/posts/new.html.erb
<div class="main posts-new"> <div class="container"> <h1 class="form-heading">投稿する</h1> <%= form_tag("/posts/create") do %> <div class="form"> <div class="form-body"> <% @post.errors.full_messages.each do |message| %> <div class="form-error"> <%= message %> </div> <% end %></div> </div><textarea name="content"><%= @post.content%></textarea> <input type="submit" value="投稿を確認"> </div> </div> <% end %>
app/views/posts/confirm.html.erb
<%= form_for @post, url: posts_path do |form| %>
<div class="field"> content: <%= @post.content %> <%= f.hidden_field :content %> </div> <div class="actions"> <%= f.submit '戻る', name: 'back'%> </div> <div class="actions"> <%= f.submit '投稿する!', name: 'post' %> </div> <% end %>app/views/posts/index.html.erb
<div class="main posts-index"> <div class="container"> <% @posts.each do |post| %> <div class="posts-index-item"> <%= link_to(post.content, "/posts/#{post.id}") %> </div> <% end %> </div> </div>config.routes.rb
Rails.application.routes.draw do
get "posts/index" => "posts#index"
get "posts/new" => "posts#new"
post "posts/confirm" => "posts#confirm"
get "posts/:id" => "posts#show"
post "posts/create" => "posts#create"
get "posts/:id/edit" => "posts#edit"
post "posts/:id/update" => "posts#update"
post "posts/:id/destroy" => "posts#destroy"
get "/" => "home#top"
get "about" => "home#about"
end
回答1件
あなたの回答
tips
プレビュー