Ruby on rails Rails未初始化常量路由错误
我知道这个问题被贴在网上,但是在看了所有的答案后,我仍然不知道我的问题是什么 这是我的路线:Ruby on rails Rails未初始化常量路由错误,ruby-on-rails,ruby,ruby-on-rails-3,routes,Ruby On Rails,Ruby,Ruby On Rails 3,Routes,我知道这个问题被贴在网上,但是在看了所有的答案后,我仍然不知道我的问题是什么 这是我的路线: resources :applications do resources :applicants do collection {put '/archived'} collection {put '/not_archived'} end end 然后,我在这个路径上有一个表:/applications/1/applications,它调用我的索引方法 在这个列出所有申请者的表中
resources :applications do
resources :applicants do
collection {put '/archived'}
collection {put '/not_archived'}
end
end
然后,我在这个路径上有一个表:/applications/1/applications
,它调用我的索引方法
在这个列出所有申请者的表中,我对每一行都有一个复选框,当他们选中该复选框时,我想使用ajax在db中为该行设置“存档”标志
以下是我的ajax调用:
$('.archive').on('click', function() {
if ($(this).is(':checked')) {
$.ajax({
type: "PUT",
url: '/applications/<%= @application.id %>/applicants/archived',
data: {id: $(this).attr('id'), archived: true}
});
} else {
$.ajax({
type: "PUT",
url: '/applications/<%= @application.id %>/applicants/not_archived',
data: {id: $(this).attr('id'), archived: false}
});
}
});
我的应用程序控制器具有以下方法:
# PUT /applicants/archived/1
def archived
p 'here?'
@applicant = Applicant.find(params[:id])
@applicant.archived = params[:archived]
#@application = current_company.applications.find(params[:application_id])
respond_to do |format|
if @applicant.save
format.js
#format.html { redirect_to @task, notice: 'Task was successfully updated.' }
#format.json { head :no_content }
else
format.html { render action: "index" }
format.json { render json: @applicant.errors, status: :unprocessable_entity }
end
end
end
# PUT /applicants/not_archived/1
def not_archived
@applicant = Applicant.find(params[:id])
@applicant.archived = params[:archived]
#@application = current_company.applications.find(params[:application_id])
respond_to do |format|
if @applicant.save
format.js
#format.html { redirect_to @task, notice: 'Task was successfully updated.' }
#format.json { head :no_content }
else
format.html { render action: "index" }
format.json { render json: @applicant.errors, status: :unprocessable_entity }
end
end
end
以下是这些路由的rake路由输出:
archived_application_applicants PUT /applications/:application_id/applicants/archived(.:format) applications/:application_id/applicants#archived
not_archived_application_applicants PUT /applications/:application_id/applicants/not_archived(.:format) applications/:application_id/applicants#not_archived
application_applicants GET /applications/:application_id/applicants(.:format) applicants#index
POST /applications/:application_id/applicants(.:format) applicants#create
new_application_applicant GET /applications/:application_id/applicants/new(.:format) applicants#new
edit_application_applicant GET /applications/:application_id/applicants/:id/edit(.:format) applicants#edit
application_applicant GET /applications/:application_id/applicants/:id(.:format) applicants#show
PUT /applications/:application_id/applicants/:id(.:format) applicants#update
DELETE /applications/:application_id/applicants/:id(.:format) applicants#destroy
是什么导致了我的错误?我完全被难住了。试试看
$('.archive').on('click', function() {
if ($(this).is(':checked')){
$.ajax({
type: "PUT",
url: '<%= archived_application_applicants_path(:application_id => @application.id) %>',
data: {id: $(this).attr('id'), archived: true}
});
}else {
$.ajax({
type: "PUT",
url: '<%= not_archived_application_applicants_path(:application_id => @application.id) %>',
data: {id: $(this).attr('id'), archived: true}
});
}
});
$('.archive')。在('click',function()上{
如果($(this).is(':checked')){
$.ajax({
键入:“放置”,
url:'@application.id)%>',
数据:{id:$(this).attr('id'),存档:true}
});
}否则{
$.ajax({
键入:“放置”,
url:'@application.id)%>',
数据:{id:$(this).attr('id'),存档:true}
});
}
});
试试看
$('.archive')。在('click',function()上{
如果($(this).is(':checked')){
$.ajax({
键入:“放置”,
url:'@application.id)%>',
数据:{id:$(this).attr('id'),存档:true}
});
}否则{
$.ajax({
键入:“放置”,
url:'@application.id)%>',
数据:{id:$(this).attr('id'),存档:true}
});
}
});
我希望此代码适用于您:
$('.archive').on('click', function() {
if ($(this).is(':checked')){
$.ajax({
type: "PUT",
url: '<%= archived_application_applicants_path(@application.id) %>',
data: {id: $(this).attr('id'), archived: true}
});
}else {
$.ajax({
type: "PUT",
url: '<%= not_archived_application_applicants_path(@application.id) %>',
data: {id: $(this).attr('id'), archived: false}
});
}
});
$('.archive')。在('click',function(){
如果($(this).is(':checked')){
$.ajax({
键入:“放置”,
url:“”,
数据:{id:$(this).attr('id'),存档:true}
});
}否则{
$.ajax({
键入:“放置”,
url:“”,
数据:{id:$(this).attr('id'),存档:false}
});
}
});
我希望此代码适用于您:
$('.archive').on('click', function() {
if ($(this).is(':checked')){
$.ajax({
type: "PUT",
url: '<%= archived_application_applicants_path(@application.id) %>',
data: {id: $(this).attr('id'), archived: true}
});
}else {
$.ajax({
type: "PUT",
url: '<%= not_archived_application_applicants_path(@application.id) %>',
data: {id: $(this).attr('id'), archived: false}
});
}
});
$('.archive')。在('click',function(){
如果($(this).is(':checked')){
$.ajax({
键入:“放置”,
url:“”,
数据:{id:$(this).attr('id'),存档:true}
});
}否则{
$.ajax({
键入:“放置”,
url:“”,
数据:{id:$(this).attr('id'),存档:false}
});
}
});
这可能与您的javascript无关。路由器找不到正确的模块/类集,无法解析向哪个控制器发送请求
我猜您的应用程序控制器的名称空间如下:
class Applications::ApplicantsController < Superclass;
end
类应用程序::应用程序控制器
但是applicators_controller.rb不在app/controllers/applications中,Rails需要在app/controllers/applications中自动创建应用程序模块。移动申请者\u controller.rb或删除名称空间,事情应该会自行解决。这可能与您的javascript无关。路由器找不到正确的模块/类集,无法解析向哪个控制器发送请求 我猜您的应用程序控制器的名称空间如下:
class Applications::ApplicantsController < Superclass;
end
类应用程序::应用程序控制器
但是applicators_controller.rb不在app/controllers/applications中,Rails需要在app/controllers/applications中自动创建应用程序模块。移动申请者\u controller.rb或删除名称空间,事情应该会自行解决。我制作了一个演示应用程序来复制您的上下文,它对我来说很好。您可以在这里查看: 我已经清理了这个问题周围的噪音,希望以此作为起点 我没有足够的声誉来评论你的问题,所以我将把它放在这里:) 我认为问题应该出在你的
app/views/applicators/archived.js.erb
中,因为你说问题是在选中复选框时出现的。你能把那个文件的内容贴出来吗
请注意,让您考虑将“归档”动作作为成员动作。您实际上是针对单个申请人而不是所有申请人采取行动:
resources :applications do
resources :applicants do
put :archived, on: :member
put :not_archived, on: :member
end
end
我制作了一个复制您的上下文的演示应用程序,它对我来说很好。您可以在这里查看: 我已经清理了这个问题周围的噪音,希望以此作为起点 我没有足够的声誉来评论你的问题,所以我将把它放在这里:) 我认为问题应该出在你的
app/views/applicators/archived.js.erb
中,因为你说问题是在选中复选框时出现的。你能把那个文件的内容贴出来吗
请注意,让您考虑将“归档”动作作为成员动作。您实际上是针对单个申请人而不是所有申请人采取行动:
resources :applications do
resources :applicants do
put :archived, on: :member
put :not_archived, on: :member
end
end
在控制器PUT/applicators/archived/1中,似乎需要将归档创建为成员路由而不是集合。然后,您需要通过ajax调用传递id。您还需要重新配置ajax路径。您是否为应用程序和申请者使用一个控制器?或者有两个控制器?它们放在哪里?看起来您的控制器PUT/applicators/archived/1中存在命名空间问题,似乎您需要将归档创建为成员路由而不是集合。然后,您需要通过ajax调用传递id。您还需要重新配置ajax路径。您是否为应用程序和申请者使用一个控制器?或者有两个控制器?它们放在哪里?看起来像是命名空间问题我不认为这是问题所在。以下是我的控制器声明:
class ApplicationController
和class applicationscocontroller
但是您正在routes.rb文件中为申请者控制器命名,所以您需要在代码中匹配它。将申请者更改为“类应用程序::应用程序控制器<应用程序控制器”,然后移动