#< CourseQuery :: ActiveRecord_Relation的未定义方法`to_key':0x007f070689bae8>
这是相关的控制器:
class Students::DashboardController < ApplicationController
before_filter :authenticate_student!
def index
@course_query=CourseQuery.all
end
def show
end
def course_queries_details
@course_queries = current_student.course_queries
@course_query = CourseQuery.new
end
end
视图,观看/学生/仪表板/ course_queries_details.html.erb:
<%= simple_form_for (@course_query) do |m| %>
<div class="modal-body">
<%= m.input :message ,label: false %>
</div>
<% end %>
配置/ routes.rb中:
namespace :students do
root to: "dashboard#show"
get 'course-queries-details', to: 'dashboard#course_queries_details'
resources :students
end
有谁能告诉我如何修复此错误?
答案 0 :(得分:1)
使用ActiveRecord时,all
方法返回所谓的关系,即多个数据库记录的集合。
使用simple_form_for
构建表单时,只能传入单个数据库记录,而不能传递整个关系。 simple_form会对传递到to_key
的内容调用simple_form_for
,因为它假定它是单个记录。
我不确定你的意图,但你可以尝试这个解决方案,它构建一个用户可以进行全新课程查询的表单:
<%= simple_form_for(CourseQuery.new) do |m| %>
<div class="modal-body">
<%= m.input :message ,label: false %>
</div>
<% end %>
答案 1 :(得分:0)
您似乎需要自定义表单网址。你必须这样做:
<强> route.rb 强>
您必须创建新的post方法来保存表单。我试试这条路线使用Rails 3:
namespace :students do
root to: "dashboard#show"
get 'course-queries-details', to: 'dashboard#course_queries_details'
post 'save-course-queries-details', as: 'save_course_queries_details', to: 'dashboard#save_course_queries_details'
resources :students
end
然后你必须创建新方法来保存表单
<%= simple_form_for @course_query, :url => students_save_course_queries_details_path, :method => :post %>