No Route Matches错误 - 使用嵌套资源控制器

时间:2013-10-31 08:07:02

标签: ruby-on-rails ruby-on-rails-3.2 routing nested

我的应用程序出现问题,当我将路径student_postulations_path放在导航栏中时崩溃:S。

我在route.rb

resources :students do
    resources :postulations
  end

我的申请说这个错误

No route matches {:controller=>"postulations"}

我的观点

<% elsif student_signed_in? %>
  <% menu_group :pull => :right do %>
    <%= menu_item "Postulaciones", student_postulations_path %>
    <%= menu_divider %>
    <%= drop_down current_student.email do %>
      <%= menu_item "Logout", destroy_student_session_path, :method => :delete %>
    <% end %>
  <% end %>
<% else %>

当我使用rake路线时:

student_postulations GET    /students/:student_id/postulations(.:format)          postulations#index
                           POST   /students/:student_id/postulations(.:format)          postulations#create
   new_student_postulation GET    /students/:student_id/postulations/new(.:format)      postulations#new
  edit_student_postulation GET    /students/:student_id/postulations/:id/edit(.:format) postulations#edit
       student_postulation GET    /students/:student_id/postulations/:id(.:format)      postulations#show
                           PUT    /students/:student_id/postulations/:id(.:format)      postulations#update
                           DELETE /students/:student_id/postulations/:id(.:format)      postulations#destroy

我的模特学生和假设:

class Student < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :nombre, :rol,
                    :prioridad, :resumen, :categoria, :foto
  # attr_accessible :title, :body

  has_many :postulations
end

class Postulation < ActiveRecord::Base
  attr_accessible :status, :student_id

  belongs_to :student
end

我不理解我的错误......我没事。请帮忙! :)

感谢

1 个答案:

答案 0 :(得分:0)

在嵌套资源路由中,您将获得

student_postulations GET    /students/:student_id/postulations(.:format) 

因此,在您的视图中,您需要使用student_postulations_path(@student)

传递@student对象

student_postulations_pathstudent_postulations_url等帮助者将学生的实例作为第一个参数student_postulations_path(@student)来查找学生的记录。

您可以从guide

获取更多信息

所以您的链接路径将是

<%= menu_item "Postulaciones", student_postulations_path(current_student) %>