ruby-on-rails - Rails 表单参数未保存到数据库

标签 ruby-on-rails database forms

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

Controller

(我只包含了新建和创建操作)

          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 服务器日志(在本地主机上运行)

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

            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)

最佳答案

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

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

在您的 create 操作中,您得到了这个:

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

但是你应该在你的参数散列中使用名称:params[:suggestion_box]

关于ruby-on-rails - Rails 表单参数未保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17930130/

相关文章:

Mysql连接2库3表查询?

php - fatal error : Cannot use object of type mysqli_result as array in. ..第 97 行

ruby-on-rails - ruby rails - 重定向到原始请求 url

ruby-on-rails - Rails 3.1 中具有相同形式的多个模型?

database - 在什么情况下我可以使用solr core

php - 根据项目类型自动计算输入日期

c# - Windows 窗体设计器重命名复制/粘贴的控件

jQuery insertBefore 和单引号问题

mysql - Rails 的 alter_table 方法有什么用?

forms - 联系表格 - 成功消息