适用于开发,但不适用于生产。当我养成习惯时。检查所有日期:
当我在heroku日志中提交时显示:
2015-08-15T03:37:10.067022+00:00 app[web.1]: Processing by HabitsController#create as HTML
2015-08-15T03:37:10.067084+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"n+tS6pcb6WUHTCdV9BZqp/nFefhbPKAE+UTUouu2x1qK0pNulK+XNT4z8AhDuwEfZ1Z860fqnqQ/YXlARyplbQ==", "habit"=>{"committed"=>["sun", "mon", "tue", "wed", "thu", "fri", "sat", ""], "date_started(2i)"=>"8", "date_started(3i)"=>"14", "date_started(1i)"=>"2015", "trigger"=>"", "action"=>"test", "target"=>"", "reward"=>"", "tag_list"=>"", "conceal"=>"0"}, "button"=>""}
然后我收到验证错误的Flash消息,因为:committed
天的存在并非真实,即使它们都已经过检查。
通过进一步的调查,我意识到我不能mark_completed
生产中的任何习惯,我认为这与上述问题有关:
2015-08-15T03:34:24.227822+00:00 heroku[router]: at=info method=POST path="/habits/sort" host=www.personalcontrolcenter.com request_id=ef56c920-dee8-4797-967b-34354ff83fbc fwd="75.90.62.187" dyno=web.1 connect=0ms service=197ms status=500 bytes=274
2015-08-15T03:34:29.713874+00:00 app[web.1]: Started PUT "/mark_completed/7" for 75.90.62.187 at 2015-08-14 23:34:29 -0400
2015-08-15T03:34:29.720020+00:00 app[web.1]: Processing by HabitsController#mark_completed as JS
2015-08-15T03:34:29.720027+00:00 app[web.1]: Parameters: {"id"=>"7"}
2015-08-15T03:34:29.924799+00:00 app[web.1]: PG::InvalidTextRepresentation: ERROR: array value must start with "{" or dimension information
2015-08-15T03:34:29.924804+00:00 app[web.1]: : UPDATE "habits" SET "committed" = $1, "completed_at" = $2, "updated_at" = $3 WHERE "habits"."id" = $4
2015-08-15T03:34:29.924807+00:00 app[web.1]: Completed 500 Internal Server Error in 204ms
2015-08-15T03:34:29.926694+00:00 app[web.1]:
2015-08-15T03:34:29.926697+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR: array value must start with "{" or dimension information
2015-08-15T03:34:29.926698+00:00 app[web.1]: : UPDATE "habits" SET "committed" = $1, "completed_at" = $2, "updated_at" = $3 WHERE "habits"."id" = $4):
2015-08-15T03:34:29.926700+00:00 app[web.1]: app/controllers/habits_controller.rb:32:in `mark_completed'
所以我认为我们只需要重写我habit_params
所在的:committed => []
。我试过这个也没有括号。
habits_controller.rb
def create
if current_user == nil
# If there is no user, store the goal values to the session.
session[:habit_date_started] = [params["habit"]["date_started(3i)"], params["habit"]["date_started(2i)"], params["habit"]["date_started(1i)"]].join('/')
session[:habit_committed] = params["habit"]["committed"].reject(&:empty?)
session[:habit_action] = habit_params[:action]
session[:habit_target] = habit_params[:target]
session[:habit_reward] = habit_params[:reward]
session[:habit_order] = habit_params[:order]
session[:habit_missed_days] = habit_params[:missed_days]
redirect_to valuation_signup_url
else
@habit = current_user.habits.build(habit_params)
if @habit.conceal == true
@habit.save_with_current_level
redirect_to @habit, notice: 'Habit was secretly created. Remember, 3 strikes and your level restarts. Good luck!'
elsif
@habit.save_with_current_level
track_activity @habit
redirect_to @habit, notice: 'Habit was successfully created. Remember, 3 strikes and your level restarts. Good luck!'
else
flash.now[:danger] = 'Required Fields: "Committed to", "Started", and "Enter Habit"'
render 'new'
end
end
end
def habit_params
params.require(:habit).permit(
:committed => [], # In development if I comment this line out it still passes
end
end
habit.rb
validates :committed, presence: true
serialize :committed, Array
def save_with_current_level
self.levels.build
self.levels.build
self.levels.build
self.levels.build
self.levels.build
self.save
end
开发数据库
t.text "committed", default: "---\n- sun\n- mon\n- tue\n- wed\n- thu\n- fri\n- sat\n"
生产数据库
committed | text[] | default '{sun,mon,tue,wed,thu,fri,sat}'::text[]
_form
<%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %>