如果acurrent_user
提交了一个习惯,那么他:committed
执行该习惯的天数如下:
分贝t.text "committed", default: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"], array: true
习惯表(用户选择天数)
习惯控制器<%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %>
习惯/指数habit_params :committed => []
如果<% habit.committed.map { |d| d.titleize[0,3] }.each do |title| %> <%= title %> <% end %>
用户提交了一个习惯(鼓励他在注册前创建一个),那么他nil
这样做的天数如下:
分贝:committed
习惯表(用户选择天数)
*习惯控制器t.text "committed", default: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"], array: true
*用户控制器
习惯/指数<%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %>
当这两个习惯第一次提交时,终端看起来像这样:
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)
但它们的不同之处在于,当用户完成注册时,我看到终端会得出如下结论:
session[:habit_committed] = [params["habit"]["committed"]]
或committed = session.delete(:habit_committed)
或@user.habits.create(committed: committed)
。我尝试了很多方法,比如
<% habit.committed.map { |d| d.titleize[0,3] }.each do |title| %> <%= title %> <% end %>
,["committed", "{{NULL}}"]
,["committed", "{NULL}"]
,["committed", "{}"]
,.join
,.split
,.inspect
,但我猜我没有使用正确的调制或格式。抱歉,如果此问题的布局看起来像
.map
便笺。第一次尝试
将db改为
.map(&:inspect).join(', ')
习惯.rb
.map {|str| "\"#{str}\""}.join(',')
习惯表(用户选择天数)
习惯控制器
ransom
*用户控制器
习惯/指数
t.text "committed", default: "---\n- sun\n- mon\n- tue\n- wed\n- thu\n- fri\n- sat\n"
产生时间:
serialize :committed, Array
最佳答案
当您为committed设置会话变量时,您将其设置为空数组,然后获取空数组作为值。
在日志中,params[“habits”][“committed”]的值已经是一个数组,因此不需要重新解析它或将它嵌套在其他方括号中
所以试试看:
habits_controller.rb session[:habity_committed]=params[“habity”][“committed”].reject(&:empty?)
关于ruby-on-rails - 如何将数组拆分为字符串MVC?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31978181/