前提・実現したいこと
railsで簡単なtwitterに似たサイトを作成しております。
そこで投稿画面から投稿確認画面への移動がうまくできていないです。
発生している問題・エラーメッセージ
No route matches [POST] "/posts/create"
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 => "confirm" else render :action => "index" end end end
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
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 %> <%= text_area_tag :content, @post.content %> <input type="submit" value="投稿を確認" class="btn btn-link"> </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>
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/27 23:53