[英]Trouble managing model ownership in rails app
在我的rails應用程序中,用戶和學校可以創建課程,並且每門課程都有一個教學大綱。
在我的課程模型中:
belongs_to :hostable, polymorphic: true
has_one :syllabus
教學大綱:
belongs_to :course
學校(和用戶):
has_many :courses, as: :hostable, :dependent => :destroy
我的路線目前設置如下:
resources :users do
resources :courses
member do
get :attending, :memberships
end
end
resources :schools do
resources :courses
member do
put :apply, :enroll
end
end
resources :syllabuses
在我的課程控制器中,我有:
def create
@course = Course.find(params[:id])
@syllabus = @course.build_syllabus(params[:syllabus])
if @syllabus.save
redirect_to @course, notice: 'Syllabus was successfully created.'
end
end
我的課程負責人有:
def show
@course = Course.find(params[:id])
@school = Course.find(params[:school_id])
@title = @course.title
unless @course.syllabus
@syllabus = @course.build_syllabus
end
end
我的課程視圖頁面有
<%= form_for (@syllabus) do |f| %>
<div> <%= f.label :syllabus %> </div>
<%= f.label :name %>
<%= f.text_field :name %> </br>
....
<%= f.submit "Save Changes" %>
<% end %>
但是,當我嘗試從學校內部的課程頁面創建教學大綱時,會得到:
“找不到沒有ID的課程”
我究竟做錯了什么? 如何重新配置路線/模型,以便用戶和學校都可以創建課程,然后可以在課程中創建課程提綱? 我嘗試將課程表模型嵌套在帶有javascript的課程模型下,但遇到了一些問題。
嘗試在課程安排中嵌套教學大綱,
resources :courses do
resource :syllabus
@school = Course.find(params[:school_id])
在我看來很可疑。
應該是
@school = School.find(params[:school_id])
甚至更容易
@school = @course.school
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.