我有这个Ajax请求,但无论如何,我仍然得到$.ajax({type: POST, url: add_timespan, data: {to_find_id: 192148455}, success:function(){alert(1);}});
事情是:
Started POST "/subgroups/519716477/add_timespan" for 127.0.0.1 at 2014-04-03 14:54:44 +0200
Processing by SubgroupsController#add_timespan as */*
Parameters: {"to_find_id"=>"192148455", "subgroup_id"=>"519716477"}
←[1m←[36mUser Load (0.0ms)←[0m ←[1mSELECT "users".* FROM "users" WHERE "users"."id" =? LIMIT 1←[0m [["id", 702273327]]
Redirected to http://localhost:3000/
Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
正如你所看到的,它启动了我的方法,甚至传递了params,但它没有完成它,因为它没有为我的子组添加一个时间跨度。
我找不到原因。 你能救我吗?
编辑: 正如Paul Richter所提到的,这是我的控制器方法:
def add_timespan
RTimespan.create(timespan_id: params[:to_find_id].to_s, subgroup_id: @subgroup.id.to_s)
end
正如arieljoud所述: 是的我有一个前过滤器:
before_action :set_subgroup, only: [:show, :edit, :update, :destroy, :remove_admin, :search_user, :remove_user, :process_add_user, :process_add_admin]
before_action :login_required
我已经编辑了我的before_action,也有:add_timespan。
before_action :set_subgroup, only: [:show, :edit, :update, :destroy, :remove_admin, :search_user, :remove_user, :process_add_user, :process_add_admin, :add_holiday]
before_action :login_required
现在我不再有重定向了,但现在却说:
Started POST "/subgroups/519716477/add_timespan" for 127.0.0.1 at 2014-04-03 15:28:24 +0200
Processing by SubgroupsController#add_timespan as */*
Parameters: {"to_find_id"=>"310026848", "subgroup_id"=>"519716477"}
←[1m←[35mUser Load (0.0ms)←[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 702273327]]
Completed 404 Not Found in 0ms
ActiveRecord::RecordNotFound (Couldn't find Subgroup without an ID):
app/controllers/subgroups_controller.rb:161:in `set_subgroup'
app/controllers/application_controller.rb:11:in `reset_timezone_to_utc'
set_subgroup方法:
def set_subgroup
@subgroup = Subgroup.find(params[:id])
end
编辑:
我评论了before_action :login_required
。你知道我现在得到了什么错误吗?
和以前一样吗?我开始变得绝望了。
@ kirti-thorat:我已经尝试了你的想法,但它仍然无效。 当我使用你的想法时,我会回到第4行的第一个Log。 你还有其他建议吗?
了解更多信息: LINK TO MY RAILS-FORUM POST
答案 0 :(得分:0)
更新set_subgroup
以解决错误ActiveRecord::RecordNotFound (Couldn't find Subgroup without an ID)
:
def set_subgroup
if params[:id].present?
@subgroup = Subgroup.find(params[:id])
elsif params[:subgroup_id].present?
@subgroup = Subgroup.find(params[:subgroup_id])
end
end
根据你的params哈希,subgroup_id
请求获得SubgroupsController#add_timespan
。
Parameters: {"to_find_id"=>"310026848", "subgroup_id"=>"519716477"}
在这种情况下,您需要传递params[:subgroup_id]
才能找到匹配的Subgroup
记录。