Rails不会将params保存到数据库中

时间:2013-07-29 17:13:43

标签: ruby-on-rails database forms

Rails新手问题......

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

对于为什么会发生这种情况的任何反馈都将非常感激。另外,请注意,此时我对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

控制器

(我只包括新的和创建动作)

          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服务器日志(在localhost上运行)

这是日志输出,显示为name和short_name添加到db的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)

2 个答案:

答案 0 :(得分:4)

正如您在日志中看到的那样,params哈希是这样的:

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

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

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

但是你应该在你的参数哈希上使用这个名字:params[:suggestion_box]

答案 1 :(得分:2)

你的参数在密钥suggestion_box下按原样进入。

您的代码使用suggestion,这不是params的内容,正如您发布的日志所示,并且通常由Rails假设。