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

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

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

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

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

1208閲覧

ル-ティングエラーを直したい

yozakura10

総合スコア8

Ruby

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

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2020/10/22 10:37

編集2020/10/22 13:18

前提・実現したいこと
prototypeの部屋を作りたい
しかし保存しようとするとル-ティングエラーが出てしますので
それをどうにかしたいです

発生している問題・エラーメッセージ
ruby on railsでの作成途中でおきました
エラーメッセージ
https://gyazo.com/9ce4c70f76cd8fc12de4daccd7d73cd8

エラー文です

Routing Error No route matches [POST] "/prototypes/new" Rails.root: /Users/kamiyaryota/Desktop/projects/protospace-31035 Application Trace | Framework Trace | Full Trace Routes Routes match in priority from top to bottom Request Parameters: {"authenticity_token"=>"iMFsxHjUGk9ZLXCQb0jmase2O/KjhjD9LuLYI7ZTQ3rC1crNukf4EkjDG3EY5WBew32FEqq/htakRgtzcsmUgA==", "title"=>"aaaa", "catct_copy"=>"aaaa", "concept"=>"aaaa", "commit"=>"保存する"} Toggle session dump Toggle env dump Response Headers: None

該当コード
routes.rbです

ruby

1Rails.application.routes.draw do 2 devise_for :users 3 root to: "prototypes#index" 4 resources :users, only: [:new, :edit, :update,] 5 resources :prototypes, only: [:new, :create] 6 7 devise_scope :user do 8 get '/users/sign_out' => 'devise/sessions#destroy' 9 end 10 11end 12

コントローラー.rbです

ruby

1class PrototypesController < ApplicationController 2 3 def index 4 5 end 6 7 def new 8 @prototype = Prototype.new 9 end 10 11 def create 12 @prototype = Prototype.create(prototype_params) 13 if @prototype.save 14 redirect_to root_path 15 else 16 render :new 17 end 18 end 19 20 21 22 23 private 24 def prototype_params 25 params.require(:prototype).permit(:title, :catct_copy, :concept).merge(user_id: current_user.id) 26 end 27 28end

model.rbです

ryby

1class Prototype < ApplicationRecord 2has_many :comments 3belongs_to :user 4 5has_one_attached :image 6 7validates :title, presence: true 8validates :catct_copy, presence: true 9validates :concept, presence: true 10validates :image, presence: true 11 12end

投稿ページです

html

1<div class="main"> 2 <div class="inner"> 3 <div class="form__wrapper"> 4 <h2 class="page-heading">新規プロトタイプ投稿</h2> 5 <%= render partial: "form" %> 6 </div> 7 </div> 8</div>

*部分テンプレートのコードです、ファイル名は_form_html_rbです*

html

1<%= form_with model: @prototype, local: true do |f|%> 2 <div class="field"> 3 <%= f.label :title, "プロトタイプの名称" %><br /> 4 <%= f.text_field :title %> 5 </div> 6 7 <div class="field"> 8 <%= f.label :catct_copy, "キャッチコピー" %><br /> 9 <%= f.text_area :catct_copy, class: :form__text %> 10 </div> 11 12 <div class="field"> 13 <%= f.label :concept, "コンセプト" %><br /> 14 <%= f.text_area :concept, class: :form__text %> 15 </div> 16 17 <div class="field"> 18 <%= f.label :image, "プロトタイプの画像" %><br /> 19 <%= f.file_field :image %> 20 </div> 21 22 <div class="actions"> 23 <%= f.submit "保存する", class: :form__btn %> 24 </div> 25<% end %>

routesの一覧です

Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create root GET / prototypes#index new_user GET /users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update prototypes POST /prototypes(.:format) prototypes#create new_prototype GET /prototypes/new(.:format) prototypes#new

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

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

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

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

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

winterboum

2020/10/22 12:57

何をしたときにそのエラーがでますか? 及び えらーは全文を載せてください
yozakura10

2020/10/22 13:02

新規投稿を保存しようとしたときです
winterboum

2020/10/22 13:05

えらーは全文を載せてください
yozakura10

2020/10/22 13:11

これで大丈夫でしょうか、あとのエラー文はroutesの中身です そちらは下に乗せているのでそちらを見ていただければと思います 細かく指摘していただきありがとうございます
guest

回答1

0

ベストアンサー

予想外のエラーメッセージでした。

newの画面を表示させるときに、ではなく submit した時に、ですから def create には行っていると思われます。
すると createの何行目で というメッセージがあると予想していたのですが、、

気になるのは
if @prototype_create.save
でもこれだとエラーは undefined method `save' for nil:NilClass だとおもうのですが。。
ということで不思議なエラーなのです。

とりあえず if @prototype.save にして試してください

投稿2020/10/22 13:13

winterboum

総合スコア23284

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

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

yozakura10

2020/10/22 13:20

上記の通り直したのですが今度は保存ボタンを押しても何もおこらなくなりました。 お手数をおかけしますがこちらのほうも教えてくださらないでしょうか
winterboum

2020/10/22 13:22

1) root_path に戻ってませんか? 2) ボタンを押した時の logを載せてください
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問