我有一个错误:nil的未定义方法`map':NilClass,我不知道如何在订阅表中删除表示法

时间:2014-04-15 06:22:32

标签: ruby-on-rails ruby activerecord ruby-on-rails-4

我不知道如何更新教师的主题并从订阅表中删除记号(记录)

teacher_controller

 def update
        @subject = Subject.where("id=?", params[:subject_id]).first
        @teacher.subjects << @subject
        respond_to do |format|
          if @teacher.update(teacher_params)
            format.html { redirect_to @teacher, notice: 'Teacher was successfully updated.' }
            format.json { head :no_content }
          else
            format.html { render action: 'edit' }
            format.json { render json: @teacher.errors, status: :unprocessable_entity }
          end
        end
      end

在下面的代码中写一下,删除老师和订阅表中的字段 - teacher_id和subject_id?

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

teachers / _form.html.erb(编辑表单)

 <%= form_for(@teacher,:html => { class: 'login-form' })  do |f| %>

    <%= select_tag "subject_id", options_from_collection_for_select(@subjects, "id", "name") %>

    <% form %>

的routes.rb

  resources :teachers do
        resources :subjects
    end

1 个答案:

答案 0 :(得分:1)

您已在控制器中定义@subject并在@subjects

中使用select_tag

将其更改为

<%= select_tag "subject_id", options_from_collection_for_select(@subject, "id", "name") %>