我创建了嵌套表单和this教程。对于新对象,一切正常,但如果我想查看编辑表单,我会收到此错误
未定义的局部变量或方法`roles_form'对于#<#:0x007f7c48ff6ab0>
形式:
= simple_form_for @match do |f|
= f.error_notification
= f.input :club_home
= f.input :club_away
= f.input :address
= f.input :date
= f.simple_fields_for :roles do |r|
%h3.text-center Referees
.duplicatable_nested_form
= r.input :role_type, collection: ["Referee", "Time/Scorekeeper", "Assessor", "Head Referee"], include_blank: false
= r.association :showcase, label_method: :name, value_method: :id, include_blank: false
- if @match.new_record?
= link_to 'Remove', '', :remote => true, :class => 'destroy_duplicate_nested_form'
- else
= link_to 'Remove', match_roles_path(@match, roles_form.object), :method => :delete, :remote => true, :class => 'destroy_duplicate_nested_form'
= roles_form.input :id, as: :hidden
= f.button :submit, class: "btn btn-primary btn-sm"
= link_to 'Add Another Referee', '', :class => 'duplicate_nested_form'
匹配控制器:
class MatchesController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
before_action :authenticate_admin, only: [:new, :create, :edit, :update, :destroy]
def index
@matches = Match.all
@matches_grid = initialize_grid(Match)
end
def show
@match = Match.find(params[:id])
@referees = @match.referees
@scorekeeper = @match.scorekeepers
@assessor = @match.assessors
end
def new
@match = Match.new
@match.roles.build
end
def create
@match = Match.new(match_params)
if @match.save
flash[:success] = "Match created"
redirect_to matches_path
else
render 'new'
end
end
def edit
@match = Match.find(params[:id])
@match.roles.build
end
def update
@match = Match.find(params[:id])
if @match.update_attributes(match_params)
flash[:success] = "Match updated"
redirect_to @match
else
render 'edit'
end
end
def destroy
@match = Match.find(params[:id])
@match.destroy
flash[:success] = "Match destroyed"
redirect_to matches_path
end
private
def match_params
params.require(:match).permit(:club_home, :club_away, :date, :address, roles_attributes: [ :id, :showcase_id, :role_type ])
end
end
路线:
Rails.application.routes.draw do
resources :matches do
resources :roles, only: :destroy
end
end
答案 0 :(得分:0)
每当您在undefined local variable or method
XYZ`中看到, it means that you are attempting to use a variable named
XYZ'等错误时,该范围内不存在该错误。
在您的情况下,您尝试使用roles_form
= roles_form.input :id, as: :hidden
但它不存在。相反,您在以下行中命名了该变量r
= f.simple_fields_for :roles do |r|
您只想将roles_form
的实例替换为r
或将r
更改为roles_form
。