如何在索引视图中显示Date::DAYNAMES?
How to Display Date::DAYNAMES In Index View?
当用户在 _form 中勾选 :days
他是 "committed to",我希望他的日子显示在 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
db
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
更新
现在索引视图将其与以下答案拉出:
["monday", "tuesday", "wednesday", "thursday", ""]
我们怎样才能让它看起来像这样?
周一、周二、周三、周四
您没有正确使用 f.check_box
,该帮助程序旨在用于单个属性,而不是用于迭代器。
你可能想要这样的东西:
<%= f.collection_check_boxes :days, Date::DAYNAMES, :downcase, :to_s %>
更新
在对问题进行一些评论和更新后,我添加了以下评论,我将其放在此处以便答案与问题相匹配:
您的迁移显示 days
是一个 datetime
字段,字符串数组无法正常工作。为了使这项工作(虽然这可能不是你想要的,并且会破坏其他东西?)你需要将该字段转换为 text
类型,即。 t.text :days
在您的迁移中,然后在您的模型中使用 serialize :days, Array
来制作序列化字段。
另一个更新
如果你检查你的日志然后你会看到这样的东西:"Unpermitted parameter: days"
- 这是因为你需要指定任何包含子结构的东西,比如数组或散列,所以而不是 :days
在你的 habit_params
你需要有 :days => []
当用户在 _form 中勾选 :days
他是 "committed to",我希望他的日子显示在 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
db
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
更新
现在索引视图将其与以下答案拉出:
["monday", "tuesday", "wednesday", "thursday", ""]
我们怎样才能让它看起来像这样?
周一、周二、周三、周四
您没有正确使用 f.check_box
,该帮助程序旨在用于单个属性,而不是用于迭代器。
你可能想要这样的东西:
<%= f.collection_check_boxes :days, Date::DAYNAMES, :downcase, :to_s %>
更新
在对问题进行一些评论和更新后,我添加了以下评论,我将其放在此处以便答案与问题相匹配:
您的迁移显示 days
是一个 datetime
字段,字符串数组无法正常工作。为了使这项工作(虽然这可能不是你想要的,并且会破坏其他东西?)你需要将该字段转换为 text
类型,即。 t.text :days
在您的迁移中,然后在您的模型中使用 serialize :days, Array
来制作序列化字段。
另一个更新
如果你检查你的日志然后你会看到这样的东西:"Unpermitted parameter: days"
- 这是因为你需要指定任何包含子结构的东西,比如数组或散列,所以而不是 :days
在你的 habit_params
你需要有 :days => []