Error "ActionController :: ParameterMissing", even with form sending parameters

0

I'm probably letting something go, but I've been trying to find the solution for hours and nowhere.

I'm creating a games CRUD. My problem is happening in the games#create action of my controller games .

Here is my implementation:

Controller "Games" (app / controllers / games_controller.rb)

class GamesController < ApplicationController

    def index
        @games = Game.all
    end

    def show
        @game = Game.find(params[:id])
        @stories = @game.stories
    end

    def new
        @game = Game.new
    end

    def create
        @game = Game.new(game_params)
        if @game.save
            redirect_to '/'
        else
            render 'new'
        end
    end

    private
    def game_params
        params.require(:title).permit(:logo, :start_screen_bg, :start_screen_bgm)
    end

end

I'm trying to execute the create action through a form in the new view of that controller.

View controller new games (app / views / games / new.html.erb)

<h1>Create New Game</h1>

<%= form_for (@game) do |g|%>
    <%= g.text_field :title %></br>
    <%= g.text_field :logo, :placeholder => "Logo URL" %></br>
    <%= g.text_field :start_screen_bg, :placeholder => "start screen bg" %></br>
    <%= g.text_field :start_screen_bgm, :placeholder => "start screen bgm" %></br>
    <%= g.submit "Create" %>
<% end %>

When I submit, the following error occurs:

ActionController::ParameterMissing in GamesController#create
param is missing or the value is empty: title
Extracted source (around line #27):
25
26
27
28
29
30

    private
    def game_params
        params.require(:title).permit(:logo, :start_screen_bg, :start_screen_bgm)
    end

end

Rails.root: E:/Projetos/

Application Trace | Framework Trace | Full Trace
app/controllers/games_controller.rb:27:in 'game_params'
app/controllers/games_controller.rb:17:in 'create'
Request
Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"3fMm6sbdqrgaYZIapN3jHb0Tq0AhslKcrVze1LWLMWpNhm2usUPCh8UXKTBg1Kzpt2Lrq5EGr+egyFxeUGuzwg==",
 "game"=>
  {"title"=>"A New Game",
   "logo"=>"https://www.google.com.br/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
   "start_screen_bg"=>"https://www.google.com.br/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
   "start_screen_bgm"=>"https://www.google.com.br/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"},
 "commit"=>"Create"}

As you can see from the "Parameters" section, the required parameter :title is being sent.

I tried to comment on the params.require(:title).permit(:logo, :start_screen_bg, :start_screen_bgm) line. The record is created successfully, but empty.

I feel it's something very simple that I'm letting go of ...

Has anyone had a similar problem? Can you help me?

Hugs!

    
asked by anonymous 31.07.2018 / 08:43

1 answer

2

Bruno, this call params require permit is called strong parameters .

In the first part, you should reference the class name. change from params.require(:title) to params.require(:game)

In the second part you should put the attributes. add title here, as shown below.

permit(:logo, :start_screen_bg, :start_screen_bgm, :title)

At the end you should have something like this

params.require(:game).permit(:logo, :start_screen_bg, :start_screen_bgm, :title)

Tip
require o Model (: game)
permit model attributes

As you yourself informs the title attribute is being sent. Right. Your page submits the title template.
But your controller is not allowing it properly.

    
31.07.2018 / 16:39