为什么我的表格不起作用?
Why my form don't work?
这是我的 tournaments_controller 文件:
class TournamentsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :destroy]
def index
end
def show
end
def new
render action: 'new'
end
def create
self.tournament = Tournament.new(tournament_params)
if tournament.save
flash[:info] = "Tournament created successfully!"
redirect_to root_url
else
render action: 'new'
end
end
def destroy
Tournament.find(params[:id]).destroy
flash[:success] = "Tournament deleted!"
redirect_to root_url
end
private
def tournament_params
params.require(:tournament).permit(:name, :maxplayers)
end
end
这是我的表单文件:
= form_for tournament do |f|
- if tournament.errors.any?
#error_explanation
%h2= "#{pluralize(tournament.errors.count, "error")} prohibited this tournament from being saved:"
%ul
- tournament.errors.full_messages.each do |msg|
%li= msg
.form-group
= f.label :name
= f.text_field :name, class: 'form-control'
= f.submit 'Save', class: 'btn btn-primary'
当我的应用程序运行时,控制台显示此错误:
undefined local variable or method `tournament' for #<#<Class:0x007f0e414fa2d0>:0x007f0e418d9a90>
如果重要的话,我会使用简单形式和 Haml。
谁能向我解释为什么会出现此错误?
您必须使用实例变量:
def new
@tournament = Tournament.new
render action: 'new'
end
def create
@tournament = Tournament.new(tournament_params)
if @tournament.save
flash[:info] = "Tournament created successfully!"
redirect_to root_url
else
render action: 'new'
end
end
和
= form_for @tournament do |f|
- if @tournament.errors.any?
#error_explanation
%h2= "#{pluralize(@tournament.errors.count, "error")} prohibited this tournament from being saved:"
%ul
- @tournament.errors.full_messages.each do |msg|
%li= msg
.form-group
= f.label :name
= f.text_field :name, class: 'form-control'
= f.submit 'Save', class: 'btn btn-primary'
= link_to 'Back', categories_path, class: 'btn btn-default'
这是我的 tournaments_controller 文件:
class TournamentsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :destroy]
def index
end
def show
end
def new
render action: 'new'
end
def create
self.tournament = Tournament.new(tournament_params)
if tournament.save
flash[:info] = "Tournament created successfully!"
redirect_to root_url
else
render action: 'new'
end
end
def destroy
Tournament.find(params[:id]).destroy
flash[:success] = "Tournament deleted!"
redirect_to root_url
end
private
def tournament_params
params.require(:tournament).permit(:name, :maxplayers)
end
end
这是我的表单文件:
= form_for tournament do |f|
- if tournament.errors.any?
#error_explanation
%h2= "#{pluralize(tournament.errors.count, "error")} prohibited this tournament from being saved:"
%ul
- tournament.errors.full_messages.each do |msg|
%li= msg
.form-group
= f.label :name
= f.text_field :name, class: 'form-control'
= f.submit 'Save', class: 'btn btn-primary'
当我的应用程序运行时,控制台显示此错误:
undefined local variable or method `tournament' for #<#<Class:0x007f0e414fa2d0>:0x007f0e418d9a90>
如果重要的话,我会使用简单形式和 Haml。 谁能向我解释为什么会出现此错误?
您必须使用实例变量:
def new
@tournament = Tournament.new
render action: 'new'
end
def create
@tournament = Tournament.new(tournament_params)
if @tournament.save
flash[:info] = "Tournament created successfully!"
redirect_to root_url
else
render action: 'new'
end
end
和
= form_for @tournament do |f|
- if @tournament.errors.any?
#error_explanation
%h2= "#{pluralize(@tournament.errors.count, "error")} prohibited this tournament from being saved:"
%ul
- @tournament.errors.full_messages.each do |msg|
%li= msg
.form-group
= f.label :name
= f.text_field :name, class: 'form-control'
= f.submit 'Save', class: 'btn btn-primary'
= link_to 'Back', categories_path, class: 'btn btn-default'