【问题标题】:No Route Matches error - with Nested Resource controller没有路由匹配错误 - 使用嵌套资源控制器
【发布时间】:2013-10-31 08:07:01
【问题描述】:

我的应用程序有问题,当我将路径 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

我不明白我的错误......我的事情还好。请帮忙! :)。

谢谢

【问题讨论】:

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


    【解决方案1】:

    在嵌套资源路由中你得到了

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

    所以在您看来,您需要使用student_postulations_path(@student) 传递@student 对象

    student_postulations_path、student_postulations_url 等助手将 Student 的实例作为第一个参数 student_postulations_path(@student) 来查找 student 的记录。

    您可以从guide获取更多信息

    所以你的链接路径将是

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

    【讨论】:

    • 谢谢! :D!你在 5 分钟票绿色中修复了我的错误 noob :) ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多