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

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

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

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

Q&A

解決済

2回答

2155閲覧

createアクションにデータ保存のための記述をし、DB(prototypesテーブル)に保存したい

0W5E8fPq1EOm4yE

総合スコア13

Ruby

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

0グッド

0クリップ

投稿2020/11/23 01:54

https://gyazo.com/30afbc80538f8ff43b513b25172b5be1
上記のURLにあるように新規投稿したものがDBに反映されずNoMethodError in PrototypesController#createと表示されました。
試したこととしましてはprototypescontrollerのcreateメソッドに誤りがないか確認しました。

prototypescontroller
class PrototypesController < ApplicationController
#CSRF保護を無効にする
protect_from_forgery with: :null_session

def index
@prototype = Prototype.includes(:user)
end

def new
@prototype = Prototype.new
end

def create
@prototype = Prototype.new(prototype_params)
if @prototype.save
redirect_to root_path
else
render :new
end
end

def show
@prototype = Prototype.find(params[:id])
end

def edit
@prototype = Prototype.find(params[:id])
end

def update
prototype = prototype.find(params[:id])
prototype.update(prototype_params)
if prototype.save
redirect_to root_path(@prototype)
else
prototype = Prototype.includes(:user)
render :edit
end
end

def destroy
prototype = Prototype.find(params[:id])
prototype.destroy
redirect_to root_path
end

private
def prototype_params
params[:prototype].permit(:title, :catch_copy, :concept, :image).merge(user_id: params[:user_id])
end
end

ターミナル
NoMethodError (undefined method `permit' for nil:NilClass):

app/controllers/prototypes_controller.rb:49:in prototype_params'
app/controllers/prototypes_controller.rb:14:increate'
Started POST "/prototypes" for ::1 at 2020-11-22 22:19:56 +0900
(1.0ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
Processing by PrototypesController#create as HTML
Parameters: {"authenticity_token"=>"qYg/uzpwEYPt9XxTgVzG3JgzHW2ILefCd2ET+i/pDnYtDGS3gINKdRNje8wWISFDYk60i9RgrwOU+gC6hRzUhQ==", "title"=>"胡蝶しのぶ", "catch_copy"=>"鬼を殺せる毒を作ったすごい人", "concept"=>"蟲柱", "image"=>#, @original_filename="胡蝶しのぶ.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name="image"; filename="\xE8\x83\xA1\xE8\x9D\xB6\xE3\x81\x97\xE3\x81\xAE\xE3\x81\xB5\xE3\x82\x99.jpeg"\r\nContent-Type: image/jpeg\r\n">, "commit"=>"保存する"}
Completed 500 Internal Server Error in 8ms (ActiveRecord: 0.0ms | Allocations: 1392)

NoMethodError (undefined method `permit' for nil:NilClass):

app/controllers/prototypes_controller.rb:49:in prototype_params'
app/controllers/prototypes_controller.rb:14:increate'

Rails.application.routes.draw do
devise_for :users
root to: "prototypes#index"
resources :prototypes, only: [:index, :new, :create, :show, :edit, :update, :destroy]
end

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

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

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

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

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

guest

回答2

0

自己解決

最終的にprototypesControllerのcreateメソッドを以下の通りにして解決しました。
def create

@prototype = Prototype.new(prototype_params) if @prototype.save redirect_to root_path(@prototype) else render :new end

end

投稿2020/11/23 10:15

0W5E8fPq1EOm4yE

総合スコア13

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

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

0

prototypes#newのviewに問題があります。


追記

_form.html.erbの

<%= form_with url:prototypes_path, method: :prototypes, local: true do |f|%>

erb

1<%= form_with model: prototype, local: true do |f|%>

にするとよいかと思います。

method: :prototypesで動いてる理由が正直分かりませんが、質問に提示されたroutes.rbならば↑で必要十分です。


uninitialized constant PrototypesController::Prototypes

質問のときとprototypes_controller.rbが変わっておりPrototypesになってる事が問題です。

投稿2020/11/23 02:51

編集2020/11/23 04:39
asm

総合スコア15147

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

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

0W5E8fPq1EOm4yE

2020/11/23 04:25

ありがとうございます。 new.html.erbから部分テンプレートである、_form.html.erbを呼び出す記述をしたのですが https://gyazo.com/3b5136d9da0f15977cf3c73b11e7c9f2 上記のようなuninitialized constant PrototypesController::Prototypesと出てしまいました。 prototypes.new.html.erb <div class="main"> <div class="inner"> <div class="form__wrapper"> <h2 class="page-heading">新規プロトタイプ投稿</h2> <%= render partial: "form", locals: { prototype: @prototype } %> </div> </div> </div> prototypes._form.html.erb <%= form_with url:prototypes_path, method: :prototypes, local: true do |f|%> <div class="field"> <%= f.label :title, "プロトタイプの名称" %><br /> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :catch_copy, "キャッチコピー" %><br /> <%= f.text_area :catch_copy, class: :form__text %> </div> <div class="field"> <%= f.label :concept, "コンセプト" %><br /> <%= f.text_area :concept, class: :form__text %> </div> <div class="field"> <%= f.label :image, "プロトタイプの画像" %><br /> <%= f.file_field :image %> </div> <div class="actions"> <%= f.submit "保存する", class: :form__btn %> </div> <% end %>
0W5E8fPq1EOm4yE

2020/11/23 07:33

ありがとうございます!頂いたアドバイスを元に修正したところ投稿した内容がDBに保存されました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問