"Actioncontroller::Parametermissing" error, even with form sending parameters

Asked

Viewed 41 times

0

I’m probably letting something really stupid go through, but I’ve been trying to find the solution for hours and nothing.

I am creating a CRUD of games. My problem is happening in the action games#create 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 carry out the action create through a form in the view new of that controller.

Game controller view "new" (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 give 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/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
   "start_screen_bg"=>"https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
   "start_screen_bgm"=>"https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"},
 "commit"=>"Create"}

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

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

I feel like it’s something very simple that I’m missing...

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

Hugs!

1 answer

2


Bruno, that call params require permit is called Strong Parameters.

In the first part, you must reference the class name.
change of params.require(:title) for params.require(:game)

in the second part you must put the attributes.
add the title here, as shown below.

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

At the end you must have something like this

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

Tip
require o Modelo (:game)
permit the attributes of the model

How you say the title attribute is being sent. Correct. Your page sends the title template.
However, your controller is not allowing it correctly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.