0W5E8fPq1EOm4yE
@0W5E8fPq1EOm4yE (Nishio)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

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

https://gyazo.com/30afbc80538f8ff43b513b25172b5be1
上記のURLにあるように新規投稿したものがDBに反映されずNoMethodError in 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:in
create'
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:in
create'

0

1Answer

viewでフォームをどのように記述しているかがわからないと回答ができなさそうですが、エラーの内容を見る感じ

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

この定義が間違っていそうです。

0Like

Your answer might help someone💌