質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1293閲覧

RailsのUrlGenerationError の対応方法について

mai0521

総合スコア22

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2019/11/28 12:42

編集2019/11/28 13:42

変更画面→確認画面→変更完了 の処理をrailsで行おうとしていますが、
現在以下のようなエラーが出ております。

ActionView::Template::Error (No route matches {:action=>"confirm", :controller=>"users", :id=>"1"}): 1: <div class="container"> 2: <div class="my-3"> 3: <%= form_for @user, url: {action: 'confirm'} do |f| %> 4: <div class="form-group col-8 d-flex pb-2"> 5: <%= f.label :name, "Name", class:"pr-2" %> 6: <%= f.text_field :name, class: "form-control text-monospace" %>
ActionController::UrlGenerationError in Users#edit No route matches {:action=>"confirm", :controller=>"users", :id=>"1"}

しかし、usersコントーラーにconfirmアクションはあり、
ぐぐってもどうしてこのようなエラーが出ているのかわかりません。
どなたかご教示いただけますでしょうか・・・?
よろしくお願いいたします。

以下ソースコードです。

users_controller.rb

class UsersController < ApplicationController before_action :fetch_userList, only: [:update, :edit, :destroy] def index @users = User.all end def new @user = User.new end def edit end def create @user = User.new(user_params) if params[:back] render :new elsif @user.save redirect_to root_path, notice:"新規登録が完了しました" else render :new end end def update if @user.update(user_params) redirect_to root_path, notice:"修正が完了しました" else render 'edit' end end def confirm @user = User.new(user_params) end def destroy @user.destroy redirect_to root_path, notice:"削除が完了しました" end private def fetch_userList @user = User.find(params[:id]) end def user_params params.require(:user).permit( :name, :twitterId, :country, :position, :date, :twitterLink, :twitterArticle, :memo ) end end

edit.html.erb

<div class="container"> <div class="my-3"> <%= form_for @user, url: {action: 'confirm'} do |f| %> <div class="form-group col-8 d-flex pb-2"> <%= f.label :name, "Name", class:"pr-2" %> <%= f.text_field :name, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :twitterId, "TwitterId" %> <%= f.text_field :twitterId, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :country, "Country" %> <%= f.text_field :country, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :position, "Position" %> <%= f.text_field :position, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :date, "Date" %> <%= f.text_field :date, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :twitterLink, "TwitterLink" %> <%= f.text_field :twitterLink, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :twitterArticle, "TwitterArticle" %> <%= f.text_field :twitterArticle, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :memo, "Memo" %> <%= f.text_field :memo, class: "form-control text-monospace" %> </div> <%= f.submit %> <% end %> </div> </div>

routes.rb

Rails.application.routes.draw do root "users#index" resources :users do post 'confirm' end end

confirm.html.erb

<div class="container"> <div class="my-3"> <%= form_for @user do |f| %> <div class="form-group col-8 d-flex pb-2"> <%= f.label :name, "Name", class:"pr-2" %> <%= f.text_field :name, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :twitterId, "TwitterId" %> <%= f.text_field :twitterId, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :country, "Country" %> <%= f.text_field :country, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :position, "Position" %> <%= f.text_field :position, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :date, "Date" %> <%= f.text_field :date, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :twitterLink, "TwitterLink" %> <%= f.text_field :twitterLink, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :twitterArticle, "TwitterArticle" %> <%= f.text_field :twitterArticle, class: "form-control text-monospace" %> </div> <div class="form-group col-8 d-flex pb-2"> <%= f.label :memo, "Memo" %> <%= f.text_field :memo, class: "form-control text-monospace" %> </div> <div class="actions"> <%= f.submit 'Back', name: 'back' </div> <%= f.submit %> <% end %> </div> </div>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

routesの書き方がまずいのかもしれません

resources :users do member do post 'confirm' end end

で試して下さい

投稿2019/11/28 14:36

winterboum

総合スコア23329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mai0521

2019/11/29 14:54

ありがとうございます!その通りでした。 member・collectionの違いも、ご回答をきっかけに学べました。本当にありがとうございます!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問