我是一个rails(和ruby)的新手并且正在努力与一个脚手架控制器,我试图调整更多我的配置 - 记录正在保存,但数据库中的字段为空 - 这是开发的结果的.log
Started POST "/events" for 127.0.0.1 at 2011-12-12 10:36:55 +0000
Processing by EventsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mizap5163W5RbRrHiWdepYZDgrsaflhJPWaVhdHlaOA=", "event"=>{"title"=>"bla de bla", "details"=>"bla de bla", "ticket_info"=>"bla de bla", "when(1i)"=>"2011", "when(2i)"=>"12", "when(3i)"=>"12", "when(4i)"=>"10", "when(5i)"=>"34", "ends(1i)"=>"2011", "ends(2i)"=>"12", "ends(3i)"=>"12", "ends(4i)"=>"10", "ends(5i)"=>"34"}, "commit"=>"Create Event"}
[1m[35m (0.0ms)[0m BEGIN
[1m[36mSQL (0.0ms)[0m [1mINSERT INTO `events` (`created_at`, `details`, `ends`, `image_url`, `location`, `organiser_id`, `ticket_info`, `title`, `updated_at`, `when`) VALUES ('2011-12-12 10:36:55', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2011-12-12 10:36:55', NULL)[0m
[1m[35m (45.0ms)[0m COMMIT
Redirected to http://localhost:3000/events/11
Completed 302 Found in 67ms
表格和控制器非常基本,所以我不知道我哪里出错了。
控制器方法 -
# GET /Events/new
# GET /Events/new.json
def new
@Event = Event.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @Event }
end
end
# GET /Events/1/edit
def edit
@Event = Event.find(params[:id])
end
# POST /Events
# POST /Events.json
def create
@Event = Event.new(params[:Event])
respond_to do |format|
if @Event.save
format.html { redirect_to @Event, notice: 'Event was successfully created.' }
format.json { render json: @Event, status: :created, location: @Event }
else
format.html { render action: "new" }
format.json { render json: @Event.errors, status: :unprocessable_entity }
end
end
end
形式(可能是非常基本的错误)
<%= form_for(@Event) do |f| %>
<% if @Event.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@Event.errors.count, "error") %> prohibited this event from being saved:</h2>
<ul>
<% @Event.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :details %><br />
<%= f.text_area :details %>
</div>
<div class="field">
<%= f.label :ticket_info %><br />
<%= f.text_area :ticket_info %>
</div>
<div class="field">
<%= f.label :when %><br />
<%= f.datetime_select :when %>
</div>
<div class="field">
<%= f.label :ends %><br />
<%= f.datetime_select :ends %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
和模型 - (我在attr_accessible标签中加入认为它有所不同)
class Event < ActiveRecord::Base
attr_accessible :title, :Details, :image_url, :organiser_id, :ticket_info, :when, :ends, :location
end
你可以从日志中看到sql查询没有插入任何数据的行,但是params是它们
答案 0 :(得分:6)
这是错误的:
def create
@Event = Event.new(params[:Event])
....
end
应该是:
def create
@Event = Event.new(params[:event])
....
end
... HF
//你也应该在cvariable名字中使用downcase。大写只应用于ClassName等常量!
答案 1 :(得分:0)
查看ruby命名约定here。
建议始终使用小写实例变量!因此,请使用@event
代替@Event
。
因此遵循rails指南(并且您的脚手架看起来相同),您的控制器create
方法应如下所示:
def create
@event = Event.new(params[:event])
respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render json: @event, status: :created, location: @event }
else
format.html { render action: "new" }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
请注意,只有类名,模块名和常量才会大写。按惯例,符号总是较低的。
希望这有帮助。