2011-12-12 99 views
1

即时通讯轨道(和红宝石)新手和挣扎与脚手架的控制器,我试图调整更多我的配置 - 记录正在保存,但数据库中的字段为空 - 这里是开发的结果.logruby​​ on rails params not being

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标签添加认为它owuld有所作为)

class Event < ActiveRecord::Base 
    attr_accessible :title, :Details, :image_url, :organiser_id, :ticket_info, :when, :ends, :location 
end 

你可以从日志中看到sql查询没有插入任何数据的行,但是PARAMS是他们

回答

6

这是错误的:

def create 
    @Event = Event.new(params[:Event]) 
    .... 
end 

它应该是:

def create 
    @Event = Event.new(params[:event]) 
    .... 
end 

HF ...

//,你也应该在cvariable名称中使用downcase。大写只能用于常量,比如ClassName!

+0

这是一个约定 - 即使form_for中的变量是大写的,参数将是小写的? (只为未来) –

+1

是的,这是正确的!您应该遵循该约定,因为它可以帮助您完成其他编码人员可以使用的应用程序!此外,它对于应用程序范围很广的部分也是有用的,不仅仅适用于一个模型! – davidb

0

查看红宝石命名约定here

建议始终使用小写的实例变量!因此请使用@event而不是@Event

所以下面轨准则(和你的支架看起来会是相同的),您的控制器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  

请注意,只有类名,模块名称和常量永远大写。按惯例,符号总是较低的。

希望这会有所帮助。