【发布时间】: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