如何将数组拆分为字符串MVC? [英] How to break up an array into strings MVC?

查看:65
本文介绍了如何将数组拆分为字符串MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果current_user养成习惯,那么他:committed的习惯就是这样的:

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. 习惯/形式 <%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %>(用户选择天数)
  3. habits_controller habit_params :committed => []
  4. 习惯/索引 <% habit.committed.map { |d| d.titleize[0,3] }.each do |title| %> <%= title %> <% end %>
  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 %>

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

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. 习惯/_形式 <%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %>(用户选择天数)
  3. * habits_controller session[:habit_committed] = [params["habit"]["committed"]]
  4. * users_controller committed = session.delete(:habit_committed) @user.habits.create(committed: committed)
  5. 习惯/索引 <% habit.committed.map { |d| d.titleize[0,3] }.each do |title| %> <%= title %> <% end %>
  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 %>

第一次提交这两种习惯时,终端看起来像这样:

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)

但是区别在于用户完成注册后,我看到终端得出以下结论:["committed", "{{NULL}}"]["committed", "{NULL}"]["committed", "{}"].

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", "{}"].

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

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.

对不起,如果该问题的布局看起来像ransom注释.

Sorry if this question's layout looks like a ransom note.

  1. db 更改为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. 习惯/形式 <%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %>(用户选择天数)
  4. habits_controller session[:habit_committed] = [params["habit"]["committed => []"]]
  5. * users_controller committed = session.delete(:habit_committed) @user.habits.create(committed: committed)
  6. 习惯/索引 <% habit.committed.map { |d| d.titleize[0,3] }.each do |title| %> <%= title %> <% end %>
  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 %>

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

推荐答案

committed 设置会话变量时,会将其设置为一个空数组,然后将其作为一个空数组.值.

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.

在您的日志中, params ["habits"] ["committed"] 的值已经是一个数组,因此无需重新解析或将其嵌套在其他方括号中

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

因此,请尝试一下:

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

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

这篇关于如何将数组拆分为字符串MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆