基本上每次页面加载此错误都会在终端中弹出。它似乎没有引起任何实际问题:
NoMethodError (undefined method `each' for nil:NilClass): app/controllers/habits_controller.rb:8:in `sort'
我想解决这个问题,但我总体上知道如何解决这个问题。
当创建新习惯而不是:order
默认为nil
时,它应默认为下一个最高整数。
HabitsController
def sort
params[:order].each do |id, order|
Habit.where(id: id).update_all(order: order)
end
render nothing: true
end
def habit_params
params.require(:habit).permit(
:user_id,
:trigger,
:tag_list,
:current_level,
:conceal,
:missed_days,
:target,
:reward,
:comment,
:commentable,
:like,
:likeable,
:action,
:order,
:date_started,
:missed_one,
:completed,
:completed_at,
:notes_text,
:notes_date,
:notable,
:note,
:committed => [],
levels_attributes: [
:missed_days,
:days_lost], notes_attributes: [:notable, :note, :notes_text, :notes_date, :_destroy])
end
模式
create_table "habits", force: true do |t|
t.integer "order"
end
rails c
[2] pry(main)> Habit.last
Habit Load (2.9ms) SELECT "habits".* FROM "habits" ORDER BY "habits"."id" DESC LIMIT 1
=> #<Habit:0x007f94e0da39b0
id: 12,
missed_days: 0,
conceal: false,
likes: nil,
date_started: Tue, 15 Sep 2015 00:00:00 EDT -04:00,
trigger: "wake up",
action: "run",
target: "2 miles",
reward: "eat dessert",
user_id: 2,
created_at: Tue, 15 Sep 2015 09:53:31 EDT -04:00,
updated_at: Tue, 15 Sep 2015 09:53:31 EDT -04:00,
order: nil,
completed_at: nil,
committed: ["sun", "mon", "tue", "wed", "thu", "fri", "sat", ""],
strike_date: nil,
missed_days_date: nil>
rails s
Started POST "/habits/sort" for 127.0.0.1 at 2015-09-15 10:02:36 -0400
Processing by HabitsController#sort as */*
User Load (2.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
Habit Load (0.4ms) SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = $1 [["user_id", 2]]
ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))
CACHE (0.0ms) SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = $1 [["user_id", 2]]
Level Load (0.3ms) SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = $1 ORDER BY "levels"."id" ASC [["habit_id", 11]]
Level Load (0.3ms) SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = $1 ORDER BY "levels"."id" ASC [["habit_id", 12]]
Level Load (0.3ms) SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = $1 ORDER BY "levels"."id" ASC [["habit_id", 10]]
ActsAsTaggableOn::Tag Load (0.3ms) SELECT DISTINCT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."tagger_id" = $1 AND "taggings"."tagger_type" = $2 ORDER BY taggings_count desc LIMIT 20 [["tagger_id", 2], ["tagger_type", "User"]]
(0.3ms) SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = $1 [["user_id", 2]]
Completed 500 Internal Server Error in 51ms
NoMethodError (undefined method `each' for nil:NilClass): app/controllers/habits_controller.rb:8:in `sort'
习惯-sort.js
var update_orders, update_remote_orders;
update_remote_orders = function(orders) {
return $.ajax({
url: "/habits/sort",
type: "POST",
data: {
order: orders
},
success: function(data) {}
});
};
update_orders = function() {
var orders;
orders = {};
$("#sortable tr input.order:hidden").each(function(i, o) {
orders[$(o).attr("data-id")] = i;
return $(o).val(i);
});
return update_remote_orders(orders);
};
$(document).ready(function() {
update_orders();
$("#sortable").sortable({
axis: 'y',
handle: ".btn",
stop: function(ui, event) {
return update_orders();
}
});
});