If a current_user
submits a habit, the days he :committed
to doing it work like this:
- db
t.text "committed", default: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"], array: true
- habits/_form
<%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %>
(user chooses days)- habits_controller
habit_params :committed => []
- habits/index
<% habit.committed.map { |d| d.titleize[0,3] }.each do |title| %> <%= title %> <% end %>
If a nil
user submits a habit (he is encouraged to create one before signing up), the days he :committed
to doing it work like this:
- db
t.text "committed", default: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"], array: true
- habits/_form
<%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %>
(user chooses days)- *habits_controller
session[:habit_committed] = [params["habit"]["committed"]]
- *users_controller
committed = session.delete(:habit_committed)
@user.habits.create(committed: committed)
- habits/index
<% habit.committed.map { |d| d.titleize[0,3] }.each do |title| %> <%= title %> <% end %>
When both habits were first submitted the terminal looks something like this:
Started POST "/habits" for 127.0.0.1 at 2015-08-11 13:15:40 -0400
Processing by HabitsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"EDxn180pxfaqNCBNtzxJd3Y0XHO5m9eURhj9WOf25Re64ed0f99HlIXIgHfNpyJIi1KD92SQ/QggcTCf7pZPHw==", "habit"=>{"committed"=>["sun", "mon", "tue", "wed", "thu", "fri", "sat", ""], "date_started(2i)"=>"8", "date_started(3i)"=>"11", "date_started(1i)"=>"2015", "trigger"=>"test", "action"=>"test", "target"=>"test", "reward"=>"test"}, "button"=>""}
Redirected to http://0.0.0.0:3000/valuation_signup
Completed 302 Found in 11ms (ActiveRecord: 0.0ms)
But where they differ is when the user finishes signing up I see the terminal conclude such things as: ["committed", "{{NULL}}"]
or ["committed", "{NULL}"]
or ["committed", "{}"]
.
I tried a variety of things such as .join
, .split
, .inspect
, .map
, .map(&:inspect).join(', ')
, .map {|str| "\"#{str}\""}.join(',')
but I'm guessing I didn't use the right concoction or format.
Sorry if this question's layout looks like a ransom
note.
1st ATTEMPT
- Changed db to
t.text "committed", default: "---\n- sun\n- mon\n- tue\n- wed\n- thu\n- fri\n- sat\n"
- habits.rb
serialize :committed, Array
- habits/_form
<%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %>
(user chooses days)- habits_controller
session[:habit_committed] = [params["habit"]["committed => []"]]
- *users_controller
committed = session.delete(:habit_committed)
@user.habits.create(committed: committed)
- habits/index
<% habit.committed.map { |d| d.titleize[0,3] }.each do |title| %> <%= title %> <% end %>
This produced: ["committed", "---\n- \n"]
When you are setting your session variable for committed you are setting it to an empty array and then are getting an empty array as the value.
In your logs the value for params["habits"]["committed"] is already an array so no need to re-parse it or nest it inside other brackets
So give this a try: