如何将数组分解为字符串MVC?

如果current_user提交了一个习惯,那么他:committed这样做的日子就是这样:

  1. db t.text "committed", default: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"], array: true
  2. 习惯/ _form (用户选择天数)
  3. habit_controller habit_params :committed => []
  4. 习惯/指数

如果一个nil用户提交了一个习惯(鼓励他在注册前创建一个习惯),那么他:committed这样做的日子是这样的:

  1. db t.text "committed", default: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"], array: true
  2. 习惯/ _form (用户选择天数)
  3. * habit_controller session[:habit_committed] = [params["habit"]["committed"]]
  4. * users_controller committed = session.delete(:habit_committed) @user.habits.create(committed: committed)
  5. 习惯/指数

在此处输入图像描述

当两个习惯首次提交时,终端看起来像这样:

 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) 

但是他们不同的地方是当用户完成注册时我看到终端总结如下: ["committed", "{{NULL}}"]["committed", "{NULL}"]["committed", "{}"]

我尝试过各种各样的东西,例如.join.split.inspect.map.map(&:inspect).join(', ') .inspect .map {|str| "\"#{str}\""}.join(',') .map {|str| "\"#{str}\""}.join(',')但我猜我没有使用正确的混合或格式。

对不起,如果这个问题的布局看起来像ransom票据。

第一次尝试

  1. db更改为t.text "committed", default: "---\n- sun\n- mon\n- tue\n- wed\n- thu\n- fri\n- sat\n"
  2. habit.rb serialize :committed, Array
  3. 习惯/ _form (用户选择天数)
  4. habit_controller session[:habit_committed] = [params["habit"]["committed => []"]]
  5. * users_controller committed = session.delete(:habit_committed) @user.habits.create(committed: committed)
  6. 习惯/指数

这产生了: ["committed", "---\n- \n"]

当您将会话变量设置为已提交时,您将其设置为空数组,然后获取一个空数组作为值。

在你的日志中, params [“habit”] [“committed”]的值已经是一个数组,所以不需要重新解析它或将其嵌入其他括号中

所以尝试一下:

habit_controller.rb session [:habit_committed] = params [“habit”] [“committed”]。reject(&:empty?)