0

I made this simple form:

<%= form_for @recipe do |f| %>
    <%= f.text_field :title, name: 'Título' %>
    <%= f.text_field :recipe_type, name: 'Tipo da Receita' %>
    <%= f.text_field :cuisine, name: 'Cozinha' %>
    <%= f.text_field :difficulty, name: 'Dificuldade' %>
    <%= f.text_field :cook_time, name: 'Tempo de Preparo' %>
    <%= f.text_area :ingredients, name: 'Ingredientes' %>
    <%= f.text_area :cook_method, name: 'Como Preparar' %>
    <%= f.submit :submit, name: 'Enviar %>
<% end %>

But when I submit it, I get this error:

 Failure/Error: params.require(:recipe).permit(:title, :recipe_type, :cuisine, :difficulty, :cook_time, :ingredients, :cook_method)

     ActionController::ParameterMissing:
       param is missing or the value is empty: recipe
     # ./app/controllers/recipes_controller.rb:23:in `recipe_params'
     # ./app/controllers/recipes_controller.rb:16:in `create'
     # ./spec/features/user_register_recipe_spec.rb:18:in `block (2 levels) in <top (required)>'

The problem is in the submit button, I need it named Enviar, but I tried every possible combination and nothing works. I tried:

<%= f.button :submit %>      Unable to find link or button "Enviar"
<%= f.submit 'Enviar' %>     I get the error of missing params
<%= f.submit :submit, name: 'Enviar' %> Unable to find link or button "Enviar"

I don't know what to do anymore. Here my Controller:

class RecipesController < ApplicationController

  def index
    @recipes = Recipe.all
  end

  def show
    @recipe = Recipe.find(params[:id])
  end

  def new
    @recipe = Recipe.new
  end

  def create
    @recipe = Recipe.new(recipe_params)
    if @recipe.save
      redirect_to recipe_path(@recipe)
    else 
      render :new
    end
  end

  private
    def recipe_params
      params.require(:recipe).permit(:title, :recipe_type, :cuisine, :difficulty, :cook_time, :ingredients, :cook_method)
    end

end

My router:

Rails.application.routes.draw do
  root to: 'recipes#index'
  get 'recipes/new' => 'recipes#new', as: 'new_recipe'
  post 'recipes/new' => 'recipes#create'
  resources :recipes
end

Edit: (Error when can't find the button)

$ rspec spec

User register recipe
  successfully (FAILED - 1)

Visitor view recipe details
  successfully
  and return to recipe list

Visitor visit homepage
  successfully
  and view recipe
  and view recipes list

Failures:

  1) User register recipe successfully
     Failure/Error: click_on 'Enviar'

     Capybara::ElementNotFound:
       Unable to find link or button "Enviar"
     # ./spec/features/user_register_recipe_spec.rb:18:in `block (2 levels) in <top (required)>'

Finished in 0.8137 seconds (files took 11.14 seconds to load)
6 examples, 1 failure

Failed examples:

rspec ./spec/features/user_register_recipe_spec.rb:4 # User register recipe successfully

Error when missings mapam :recipe

rspec spec

User register recipe
  successfully (FAILED - 1)

Visitor view recipe details
  successfully
  and return to recipe list

Visitor visit homepage
  successfully
  and view recipe
  and view recipes list

Failures:

  1) User register recipe successfully
     Failure/Error: click_on 'Enviar uma receita'

     SyntaxError:
       /home/italo/Área de Trabalho/code-saga/3/cookbook_parte4/app/views/recipes/new.html.erb:11: syntax error, unexpected tLABEL, expecting '='
       ...buffer.append=( f.submit, name:'Enviar' );@output_buffer.saf...
       ...                          ^~~~~
     # ./spec/features/user_register_recipe_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 0.35119 seconds (files took 1.09 seconds to load)
6 examples, 1 failure

Failed examples:

rspec ./spec/features/user_register_recipe_spec.rb:4 # User register recipe successfully
2
  • Show the error trace from your console. Commented Oct 8, 2019 at 3:29
  • I'm not sure these are the error trace(I'm not sure where I get them) but I edited adding the console error I get when I run 'rspec spec' Commented Oct 8, 2019 at 3:39

1 Answer 1

4

You are overriding the name attribute on all of your form fields, which results in an empty recipe params hash. I'm assuming this is by mistake and these are meant to be labels.

Remove all of the name:'s from your form fields and use labels instead. ie...

<%= f.label :title, "Título" %>
<%= f.text_field :title %> 

As for the button, use <%= f.submit 'Enviar' %>

Read up on Form Helpers

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.