“没有路由匹配{:action =>”show“,:controller =,:id => nil}缺少必需的键:[:id]”从另一个文件夹渲染部分时出错

问题描述 投票:0回答:1

我在app / views / school / teachers / _sub_tabs.html.haml文件中有部分内容。我在app / views / school / teachers / form.html.haml中渲染了这个部分,它完美无缺。但是当我尝试在components / student / app / views / subjects / students / index.html.haml中渲染它时,它会给出错误:No route matches {:action=>"show", :controller=>"school/teachers", :id=>nil} missing required keys: [:id]

错误:

#tertiary-slider.pills-container

  %span

#tertiary-slider.pills-container
  %span
    = link_to t('.add_names'), teachers_path(@teacher), class: selected_tab[:add_names]
  %span
    = link_to t('.add_subject'),subjects_student_path(@resource), class: selected_tab[:add_subject]

它在行“add_names”选项卡上给出错误

  1. app/views/school/teachers/_sub_tabs.html.haml
#tertiary-slider.pills-container
  %span
    = link_to t('.add_names'), teacher_path(@teacher), class: selected_tab[:add_names]
  %span
    = link_to t('.add_subject'),subjects_student_path(@resource), class: selected_tab[:add_subject]

在这里@resource可以是teacherstudent

  1. components/student/app/views/subjects/students/index.html.haml
.col-xs-12.well-bordered
  = render 'school/teachers/sub_tabs', { selected_tab: { setting: 'selected' } }

我认为它无法在@teacher找到subject/students。但我已经在Subjects::Students控制器中返回动作:

    def find_resource
      @resource = if params[:teacher_id]
        teacher.find(params[:teacher_id])
      else
        @student
      end
    end

路线如下:

    scope '/school' do
      resources :teachers, module: :school
      namespace :subjects do
        resources :students
      end
    end

当我检查时,传递的资源是student而不是teacher。如何通过q​​azxswpoi对象? teacherNames都属于Subjects

谢谢

编辑1:

我试图在路径中传递teacher对象:

#tertiary-slider.pills-container

  %span

#tertiary-slider.pills-container
  %span
    = link_to t('.add_names'), teachers_path(@teacher), class: selected_tab[:add_names]
  %span
    = link_to t('.add_subject'),subjects_student_path(@teacher), class: selected_tab[:add_subject]

但即便如此,老师在@teacher控制器中也没有。现在我得到的错误是:

Not found
ruby-on-rails view render partial-views
1个回答
1
投票

在你的部分你有Subjects::Students但在你的控制器中你只是将变量分配给@teacher。添加teacher

编辑:另外,当你渲染部分时,你可以通过@像这样:@teachers在部分你可以用= render 'school/teachers/sub_tabs', { teachers: @teachers, selected_tab: { setting: 'selected' } }访问它。

© www.soinside.com 2019 - 2024. All rights reserved.