我有一个使用此代码的模型中的所有对象的下拉菜单:
<h2>Upload Grades Using "New Grades"</h2>
<%= collection_select :course, :course_id, Course.all, :id,
:name, :prompt => "Select a Course:" %>
<%= link_to 'New Grade', new_grade_path(:course => :course ) %>
在这种情况下,我的模型是课程。如何访问用户的选择,然后将其传递给 new.html.erb 视图。据我所知,我使用控制器来检索值,例如
@course = params[:course][:course_id]
然后在我看来,我可以显示课程的名称
<%= @course.name %>
在 new.html.erb 内部。
然后,如果课程有一个像 A course has_many students 这样的关联,那么我可以检索并显示所有学生的列表,这些学生正在做类似的事情
@students = @course.students
但是,我的想法是错误的,因为我的代码不起作用。有人可以指出我在这里出错的地方吗?我已经在这几个小时了...
更新 - 包括 config/routes.rb 和 rake 路由
MygradesSr::Application.routes.draw do
resources :evals
resources :teams
resources :categories
resources :grades
resources :categories
resources :tasks
resources :students
resources :courses
post 'grades/upload_grade'
post 'students/upload_student'
root :to => "home#index"
end
耙路线:
evals GET /evals(.:format) evals#index
POST /evals(.:format) evals#create
new_eval GET /evals/new(.:format) evals#new
edit_eval GET /evals/:id/edit(.:format) evals#edit
eval GET /evals/:id(.:format) evals#show
PUT /evals/:id(.:format) evals#update
DELETE /evals/:id(.:format) evals#destroy
teams GET /teams(.:format) teams#index
POST /teams(.:format) teams#create
new_team GET /teams/new(.:format) teams#new
edit_team GET /teams/:id/edit(.:format) teams#edit
team GET /teams/:id(.:format) teams#show
PUT /teams/:id(.:format) teams#update
DELETE /teams/:id(.:format) teams#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
grades GET /grades(.:format) grades#index
POST /grades(.:format) grades#create
new_grade GET /grades/new(.:format) grades#new
edit_grade GET /grades/:id/edit(.:format) grades#edit
grade GET /grades/:id(.:format) grades#show
PUT /grades/:id(.:format) grades#update
DELETE /grades/:id(.:format) grades#destroy
GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
GET /categories/new(.:format) categories#new
GET /categories/:id/edit(.:format) categories#edit
GET /categories/:id(.:format) categories#show
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
tasks GET /tasks(.:format) tasks#index
POST /tasks(.:format) tasks#create
new_task GET /tasks/new(.:format) tasks#new
edit_task GET /tasks/:id/edit(.:format) tasks#edit
task GET /tasks/:id(.:format) tasks#show
PUT /tasks/:id(.:format) tasks#update
DELETE /tasks/:id(.:format) tasks#destroy
students GET /students(.:format) students#index
POST /students(.:format) students#create
new_student GET /students/new(.:format) students#new
edit_student GET /students/:id/edit(.:format) students#edit
student GET /students/:id(.:format) students#show
PUT /students/:id(.:format) students#update
DELETE /students/:id(.:format) students#destroy
courses GET /courses(.:format) courses#index
POST /courses(.:format) courses#create
new_course GET /courses/new(.:format) courses#new
edit_course GET /courses/:id/edit(.:format) courses#edit
course GET /courses/:id(.:format) courses#show
PUT /courses/:id(.:format) courses#update
DELETE /courses/:id(.:format) courses#destroy
grades_upload_grade POST /grades/upload_grade(.:format) grades#upload_grade
students_upload_student POST /students/upload_student(.:format) students#upload_student
root / home#index