1

Rails新手问题...

我有一个提交没有错误的表单,但表单参数没有保存到数据库中。但是,通过 rails 控制台添加数据可以正常工作。目前,当提交表单时,数据以 nil 形式输入到数据库中。

任何关于为什么会发生这种情况的反馈都将非常感激。另外,请注意,此时我只关心 SuggestionBox 的两个值是 name 和 short_name。

提前谢谢您的帮助!

模型

*注意:此时我关心的唯一值是 name 和 short_name*

            # == Schema Information
            #
            # Table name: suggestion_boxes
            #
            #  id              :integer          not null, primary key
            #  name            :string(255)
            #  created_at      :datetime         not null
            #  updated_at      :datetime         not null
            #  passcode        :string(255)
            #  suggestion_id   :integer
            #  organization_id :integer
            #  short_name      :string(255)
            #  owner_email     :string(255)
            #  owner_name      :string(255)
            #

            class SuggestionBox < ActiveRecord::Base
              attr_accessible :name , :passcode, :short_name
              belongs_to :organization
              has_many :suggestions, :dependent => :destroy 
            end

控制器

(我只包括了 new 和 create 动作)

          class SuggestionBoxesController < ApplicationController

            before_filter :authenticate

            def new
              @suggestion_box = SuggestionBox.new

              respond_to do |format|
                format.html # new.html.erb
                format.json { render json: @suggestion_box }
              end
            end

            def create
              @suggestion_box = SuggestionBox.new(params[:suggestion])

              respond_to do |format|
                if @suggestion_box.save
                  format.html { redirect_to @suggestion_box, notice: "Your suggestion box has been created." }
                  format.json { render json: @suggestion_box, status: :created, location: @suggestion_box }
                else
                  format.html { render action: "new" }
                  format.json { render json: @suggestion_box.errors, status: :unprocessable_entity }
                end
              end
            end
          end

看法

这是在 new.html.erb 中使用的 _form.html.erb 文件

    <%= form_for(@suggestion_box) do |f| %>
      <% if @suggestion_box.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@suggestion_box.errors.count, "error") %> prohibited this suggestion_box from being saved:</h2>

          <ul>
          <% @suggestion_box.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
          </ul>
        </div>
      <% end %>

      <div class="field">
        <%= f.label :name %><br />
        <%= f.text_field :name %>
      </div>
        <div class="field">
        <%= f.label :short_name %><br />
        <%= f.text_field :short_name %>
      </div>
      <div class="field">
        <%= f.label :passcode %><br />
        <%= f.text_field :passcode %>
      </div>
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>

Rails 服务器日志(在本地主机上运行)

这是日志输出,显示为 name 和 short_name 添加到数据库中的 nil 值。

            Started GET "/suggestion_boxes/new" for 127.0.0.1 at 2013-07-29 12:34:23 -0400
            Processing by SuggestionBoxesController#new as HTML
              Rendered suggestion_boxes/_form.html.erb (164.7ms)
              Rendered suggestion_boxes/new.html.erb within layouts/application (166.6ms)
              Rendered layouts/_flashes.html.haml (0.2ms)
            Completed 200 OK in 175ms (Views: 173.9ms | ActiveRecord: 0.0ms)


            Started GET "/assets/screen.css?body=1" for 127.0.0.1 at 2013-07-29 12:34:23 -0400
            Served asset /screen.css - 304 Not Modified (0ms)


            Started POST "/suggestion_boxes" for 127.0.0.1 at 2013-07-29 12:34:40 -0400
            Processing by SuggestionBoxesController#create as HTML
              Parameters: {"utf8"=>"✓", "authenticity_token"=>"vpzqsKOA4PLgDJXgb8s28tktcXermcB/+CrQZNMhGCI=", "suggestion_box"=>{"name"=>"testy box", "short_name"=>"tsty", "passcode"=>"123"}, "commit"=>"Create Suggestion box"}
               (0.1ms)  begin transaction
              SQL (72.4ms)  INSERT INTO "suggestion_boxes" ("created_at", "name", "organization_id", "owner_email", "owner_name", "passcode", "short_name", "suggestion_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", Mon, 29 Jul 2013 16:34:40 UTC +00:00], ["name", nil], ["organization_id", nil], ["owner_email", nil], ["owner_name", nil], ["passcode", nil], ["short_name", nil], ["suggestion_id", nil], ["updated_at", Mon, 29 Jul 2013 16:34:40 UTC +00:00]]
               (3.3ms)  commit transaction
            Redirected to http://localhost:3000/suggestion_boxes/6
            Completed 302 Found in 82ms (ActiveRecord: 75.8ms)
4

2 回答 2

4

正如您在日志中看到的那样,params 散列如下所示:

"suggestion_box"=>{"name"=>"testy box", "short_name"=>"tsty", "passcode"=>"123"}

在你的create行动中,你得到了这个:

@suggestion_box = SuggestionBox.new(params[:suggestion])

但是您应该在参数哈希上使用该名称:params[:suggestion_box]

于 2013-07-29T17:19:00.257 回答
2

您的参数在 key 下按应有的方式进入suggestion_box

您的代码使用suggestion,这不是参数是什么,如您发布的日志所示,并且通常由 Rails 假设。

于 2013-07-29T17:16:17.230 回答