我剛剛完成向我的Ruby on Rails應用程序添加嵌套資源。在rails視圖中調用變量
這主要是猜測,複製和粘貼。
現在一切正常。
但我擔心的是,在使用網址幫手,我不得不用兩個不同的變量形式 - 一個以@在開始時,一個沒有@。
在我的部分,我用:
=> link_to t('ui.edit'), edit_course_lesson_path(@course, lesson)
萬一我以前教訓添加 '@',我得到了以下錯誤:
Routing Error
No route matches {:action=>"edit", :controller=>"lessons", :course_id=>#<Course id: 2,
title: "title x", user_id: 1, subject_id: 1, student_level_id: 1, objectives: "obj xx",
created_at: "2013-08-09 15:51:38", updated_at: "2013-08-09 15:51:38">, :id=>nil}
Try running rake routes for more information on available routes.
在我定期訪問量:
=> link_to t('ui.edit'), edit_course_lesson_path(@course, @lesson)
最後一個例子,如果我寫當然沒有「@」,然後我得到:
undefined local variable or method `course' for #<#<Class:0x007f5e10082800>:0x007f5e10a64760>
只是想知道添加或意見省略「@」的區別。可能這裏的關鍵因素是部分 vs 常規視圖。
非常感謝!
PS:另外,我發現在嵌套資源主題這篇文章有用:
http://blog.8thcolor.com/2011/08/nested-resources-with-independent-views-in-ruby-on-rails/