我做了一个简单的表格:
<%= 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 %>
但是当我提交它时,出现此错误:
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)>'
问题出在提交按钮上,我需要将其命名为Enviar,但是我尝试了所有可能的组合,但没有任何效果。 我尝试过:
<%= 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"
我不知道该怎么办了。 这是我的控制器:
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
我的路由器:
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
编辑: (在找不到按钮时出错)
$ 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
缺少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
答案 0 :(得分:3)
您将覆盖所有表单字段上的name属性,这将导致空的食谱参数哈希。我以为这是错误的,这些都是标签。
从表单字段中删除所有name:
,并改用标签。即...
<%= f.label :title, "Título" %>
<%= f.text_field :title %>
对于按钮,请使用<%= f.submit 'Enviar' %>