首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么对未赋值变量没有错误?

为什么对未赋值变量没有错误?
EN

Stack Overflow用户
提问于 2014-03-22 07:21:02
回答 1查看 120关注 0票数 0

我有一个小学生模型和一个小组模型。当添加一个新的瞳孔时,我有一个collection_select框,其中有:multiple=> true,这样就可以将瞳孔分成几组。

代码语言:javascript
复制
<div class="field">
    <%= f.label "All Groups" %><br />
    <%= collection_select(:groups, :id, @all_groups,
                          :id, :name, {}, 
                          {:multiple => true}) %>
</div>

我有一个编辑瞳孔形式,当加载时,选择以前分配给瞳孔的组,以便在需要时可以更改它们,在集合选择选项中具有{}的额外位;

代码语言:javascript
复制
  <div class="field">
    <%= f.label "All Groups" %><br />
    <%= collection_select(:groups, :id, @all_groups,
                          :id, :name, {selected: @previous_selection},
                          {:multiple => true}) %>
</div>

@previous_selection设置在pupils_controller中;

代码语言:javascript
复制
  @previous_selection = Array.new
    @pupil.groups.each do |pg|
    @previous_selection.push(pg.id)
end

这是在def编辑块中,因此只为编辑页设置。

这里是PupilsController;

代码语言:javascript
复制
class PupilsController < ApplicationController
before_action :set_pupil, only: [:show, :edit, :update, :destroy]

def index
@pupils = Pupil.all
end

def show
  @pupil_groups = @pupil.groups
end


def new
@pupil = Pupil.new
@all_groups = set_pupil_list
end

def edit
  @all_groups = set_pupil_list

  @previous_selection = Array.new
   @pupil.groups.each do |pg|
  @previous_selection.push(pg.id)
end
end


def create
@pupil = Pupil.new(pupil_params)
clean_select_multiple_params
logger.debug "The groups parameter contains: #{params[:groups][:id]}"
selected_groups = Group.find(params[:groups][:id])
@pupil.groups = selected_groups

respond_to do |format|
  if @pupil.save
    format.html { redirect_to @pupil, notice: 'Pupil was successfully created.' }
    format.json { render action: 'show', status: :created, location: @pupil }
  else
    format.html { render action: 'new' }
    format.json { render json: @pupil.errors, status: :unprocessable_entity }
  end
end
end

def update
respond_to do |format|
  if @pupil.update(pupil_params)
    clean_select_multiple_params
    selected_groups = Group.find(params[:groups][:id])
    @pupil.groups = selected_groups
    format.html { redirect_to @pupil, notice: 'Pupil was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: 'edit' }
    format.json { render json: @pupil.errors, status: :unprocessable_entity }
  end
end
end

def destroy
@pupil.destroy
respond_to do |format|
  format.html { redirect_to pupils_url }
  format.json { head :no_content }
end
end

def full_name
@fn = @pupil.given_name
@sn = @pupil.surname
@full_name = @fn + @sn
end

private

def set_pupil
  @pupil = Pupil.find(params[:id])
end

def set_pupil_list
Group.all
end


def clean_select_multiple_params hash = params
hash.each do |k, v|
  case v
  when Array then v.reject!(&:blank?)
  when Hash then clean_select_multiple_params(v)
end
end
end

def pupil_params
  params.require(:pupil).permit(:given_name, :surname, :date_of_birth, :gender, :ethnicity)
end
end

当请求新的瞳孔页时,将使用_form.html.erb文件,该文件中包含{selected:@previous_selection}参数,而def在pupils_controller中尚未设置该参数,但没有关于未初始化@previous_selection的错误消息。

我期待一个错误,但没有得到一个,但不明白为什么。谁能解释一下吗?我对编程很陌生,所以如果我使用了错误的终端,很抱歉。

谢谢

里昂

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-22 07:56:25

@previous_selection变量是nil,因此在视图中不会选择任何collection项。没有必要将variable初始化为nilrails会这样做。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22574699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档