【问题标题】:How to Display Date::DAYNAMES In Index View?如何在索引视图中显示日期::DAYNAMES?
【发布时间】:2015-02-13 04:45:00
【问题描述】:

当用户在 _form 中勾选:days 他“承诺”时,我希望他的日子出现在 index 中,但目前当用户加载索引页面<%= habit.days %> 出现空白,我看到当用户单击提交时,复选标记消失。

_form

  <%= f.label "Committed to:" %>
  <% Date::DAYNAMES.each do |day| %>
    <%= f.check_box :days, {}, day %>
    <%= day %>
  <% end %>

索引

<% @habits.each do |habit| %>
  <td><%= habit.days %></td>
<% end %>

我需要向控制器或模型添加代码吗?

控制器

class HabitsController < ApplicationController
  before_action :set_habit, only: [:show, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @habits = Habit.all
  end

  def show
  end

  def new
    @habit = current_user.habits.build
  end

  def edit
  end

  def create
    @habit = current_user.habits.build(habit_params)
    if @habit.save
      redirect_to @habit, notice: 'Habit was successfully created.'
    else
      render action: 'new'
    end
  end

  def update
    if @habit.update(habit_params)
      redirect_to @habit, notice: 'Habit was successfully updated.'
    else
      render action: 'edit'
    end
  end

  def destroy
    @habit.destroy
    redirect_to habits_url
  end

  private
    def set_habit
      @habit = Habit.find(params[:id])
    end

    def correct_user
      @habit = current_user.habits.find_by(id: params[:id])
      redirect_to habits_path, notice: "Not authorized to edit this habit" if @habit.nil?
    end

    def habit_params
      params.require(:habit).permit(:missed, :left, :level, :days, :date_started, :trigger, :action, :target, :positive, :negative)
    end
end

型号

class Habit < ActiveRecord::Base
	belongs_to :user
	validates :action, presence: true
end

数据库

class CreateHabits < ActiveRecord::Migration
  def change
    create_table :habits do |t|
      t.string :missed
      t.datetime :left
      t.string :level
      t.datetime :days
      t.datetime :date_started
      t.string :trigger
      t.string :action
      t.string :target
      t.string :positive
      t.string :negative
      t.boolean :mastered

      t.timestamps null: false
    end
  end
end
                                            
                                            

更新

现在索引视图用下面的答案得出这个结论:

[“星期一”、“星期二”、“星期三”、“星期四”、“”]

我们怎样才能让它看起来像这样?

周一、周二、周三、周四

【问题讨论】:

    标签: ruby-on-rails ruby date


    【解决方案1】:

    您没有正确使用f.check_box,该帮助程序旨在用于单个属性,而不是在迭代器中使用。

    你可能想要这样的东西:

    <%= f.collection_check_boxes :days, Date::DAYNAMES, :downcase, :to_s %>
    

    更新

    在一些 cmets 和对问题的更新之后,我添加了以下评论,我将其放在这里,以便答案与问题相符:

    您的迁移表明 days 是一个 datetime 字段,字符串数组无法正常工作。要完成这项工作(尽管这可能不是您想要的,并且会破坏其他东西?)您需要将该字段转换为 text 类型,即。 t.text :days 在你的迁移中,然后在你的模型中使用 serialize :days, Array 来制作一个序列化的字段。

    另一个更新

    如果您检查您的日志,那么您会看到类似:"Unpermitted parameter: days" - 这是因为您需要指定任何包含子结构(如数组或哈希)的内容,因此在您的 @ 中而不是 :days 987654331@你需要有:days =&gt; []

    【讨论】:

    • 谢谢smathy!但是当我用你的代码替换我的代码时,我收到了这个错误消息: undefined method `collection_check_box' for #<:helpers::formbuilder:0x007fcd4c9ba750>
    • 这是我最后一个提供更多背景信息的问题:stackoverflow.com/questions/28402293/…
    • 对不起,应该是check_boxes
    • 因为需要存储数组本身,而不是字符串,并且serialize 将数组转换为一大块 YAML,很容易超出数据库中的字符串字段。另外,我再次用您的代码中的另一个问题更新了我的答案。
    • habit.days.map { |d| d.titleize[0,3] }.join ', '
    猜你喜欢
    • 2022-11-17
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多