我厌倦了拉扯我的头发......有人可以告诉我这段代码发生了什么吗?
基本上,我只是想从表单中获取数据。我检查了数百次我的控制器、模型和视图,但没有机会让事情正常工作。尽管我填写了所有字段,但我的调试 @program 结果总是这样:空白。
--- !ruby/object:Program attributes:
id:
title:
content:
active:
created_at:
updated_at:
以下是代码:
控制器:
class ProgramsController < ApplicationController
def index
@program = Program.all
end
def show
@program = Program.find(params[:id])
end
def create
@program = Program.new
if @program.save
redirect_to 'index'
else
render 'new'
end
end
def new
@program = Program.new
end
def edit
end
def destroy
end
end
模型:
class Program < ActiveRecord::Base
attr_accessible :active, :content, :title
validates :title, presence: true
validates :content, presence: true
end
看法:
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Program Adding</title>
</head>
<body>
<% if @program.errors.any? %>
<% @program.errors.full_messages.each do |msg| %>
<%= msg %>
<% end %>
<% end %>
<%= form_for(@program) do |p|%>
<%= p.label :title%><br />
<%= p.text_field :title %><br />
<%= p.label :content%><br />
<%= p.text_area :content %><br />
<%= p.label :active%><br />
<%= p.text_field :active %><br />
<%= p.submit %>
<% end %>
<%= debug @program %>
</body>
</html>
谢谢