How to break up an array into strings MVC?

2019-07-07 19:47发布

If a current_user submits a habit, the days he :committed to doing it work like this:

  1. db t.text "committed", default: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"], array: true
  2. habits/_form <%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %> (user chooses days)
  3. habits_controller habit_params :committed => []
  4. 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:

  1. db t.text "committed", default: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"], array: true
  2. habits/_form <%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %> (user chooses days)
  3. *habits_controller session[:habit_committed] = [params["habit"]["committed"]]
  4. *users_controller committed = session.delete(:habit_committed) @user.habits.create(committed: committed)
  5. habits/index <% habit.committed.map { |d| d.titleize[0,3] }.each do |title| %> <%= title %> <% end %>

enter image description here

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

  1. Changed db to t.text "committed", default: "---\n- sun\n- mon\n- tue\n- wed\n- thu\n- fri\n- sat\n"
  2. habits.rb serialize :committed, Array
  3. habits/_form <%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %> (user chooses days)
  4. habits_controller session[:habit_committed] = [params["habit"]["committed => []"]]
  5. *users_controller committed = session.delete(:habit_committed) @user.habits.create(committed: committed)
  6. habits/index <% habit.committed.map { |d| d.titleize[0,3] }.each do |title| %> <%= title %> <% end %>

This produced: ["committed", "---\n- \n"]

1条回答
三岁会撩人
2楼-- · 2019-07-07 19:58

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:

habits_controller.rb session[:habit_committed] = params["habit"]["committed"].reject(&:empty?)

查看更多
登录 后发表回答