Ruby on rails 如何使用collection_select?
我想使用collection_select,我做了一些研究,让它显示带有正确对象集合的下拉菜单,我可以选择我选择的特定对象。但从那里我不知道如何通过它 这是我的代码:Ruby on rails 如何使用collection_select?,ruby-on-rails,ruby,activerecord,views,controllers,Ruby On Rails,Ruby,Activerecord,Views,Controllers,我想使用collection_select,我做了一些研究,让它显示带有正确对象集合的下拉菜单,我可以选择我选择的特定对象。但从那里我不知道如何通过它 这是我的代码: <%= collection_select :course, :course_id, Course.all, :id, :name, :prompt => "Select a Course:" %> <%= link_to 'New Grade', new_grade_path(:cours
<%= collection_select :course, :course_id, Course.all, :id, :name, :prompt => "Select a Course:" %>
<%= link_to 'New Grade', new_grade_path(:course => :course_id ) %>
另外,如果我要在“new.html.erb”视图中显示此代码,我会使用此代码吗
<%= @course.name %>
编辑:
我认为将我的联想包括在内可能会有所帮助:
class Grade < ActiveRecord::Base
belongs_to :course
belongs_to :task
belongs_to :student
end
class Course < ActiveRecord::Base
has_many :students, :dependent => :destroy
has_many :grades
has_many :tasks, :through => :grade
has_many :teams
end
class Task < ActiveRecord::Base
belongs_to :course
has_many :grades
has_many :categories
has_one :eval
end
等级:毁灭
你有很多成绩吗
有很多:任务,:到=>:成绩
有很多队吗
结束
类任务
我想做的是在views/grades/index.html.erb页面中创建下拉菜单,以便用户可以在该课程中选择课程和任务,因此当用户单击“输入新成绩”时,它会将用户在下拉菜单中选择的参数传递到views/grades/new.html.erb,这样我就可以在链接到“输入新成绩”的new.html.erb表单中显示课程名称和我试图上传成绩的任务。您应该在视图页面上创建表单,将参数传递给控制器 views/grades/index.html.erb
<%= form_tag(new_grade_path, method: 'get') do %>
<%= label_tag "Courses" %>
<%= select_tag(
:choose_course,
options_from_collection_for_select(Course.all, "id", "name")
) %>
<%= submit_tag "Choose course" %>
<% end -%>
def new
@course = Course.find(params[:choose_course])
end
然后在视图/grades/new.html.erb中,您可以使用
@course.name
显示用户在上一页上选择的课程。但是,我认为这不适合我的情况。我想在我的索引视图中选择一门课程,这样当我进入我的新视图时,我可以使用该课程对象来显示课程中的学生。然后我可以上传课程中所有学生的分数@alexI不理解你,Ejay。请再解释一遍。很抱歉,我已经为您更新了问题。我希望这有助于更好地理解我的问题。多谢各位@Alex明白了,请看更改后的答案我认为你问题的标题有误导性,除非我遗漏了什么,否则看起来你并不是在问如何使用collection\u select
。虽然有点难说你想要什么。
def new
@course = Course.find(params[:choose_course])
end