Home>

https://gyazo.com/30afbc80538f8ff43b513b25172b5be1
As shown in the above URL, the newly posted one was not reflected in the DB and was displayed as NoMethodError in PrototypesController # create.
As for what I tried, I checked if there was an error in the create method of the prototypes controller.

prototypes controller
class PrototypesController Disable CSRF protection

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 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 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

Terminal
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 ==", "catch concept "=>" Bug pillar "," image "=>#, @original_filename =" Kocho Shinobu.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 "=>" Save "} 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

  • Answer # 1

    Finally, I solved it by changing the create method of prototypesController as follows.
    def create

    @prototype = Prototype.new (prototype_params)

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

  • Answer # 2

    prototypes # newThere is a problem with the view of.


    Postscript

    Of _form.html.erb

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

    To

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

    I think it should be.

    method :: prototypesI honestly don't know why it works, but it was presented in the questionroutes.rbThen ↑ is necessary and sufficient.


    uninitialized constant PrototypesController :: Prototypes

    When asking a questionprototypes_controller.rbHas changed and PrototypesThe problem is that it is.