繁体   English   中英

Rails表单参数未保存到数据库

[英]Rails form params not being saved to database

Rails新手问题...

我有一个提交的表单没有错误,但是表单参数没有保存到数据库中。 但是,通过Rails控制台添加数据效果很好。 当前,提交表单时,数据以nil形式输入数据库。

任何关于为什么发生这种情况的反馈将不胜感激。 另外,请注意,此时我所建议的SuggestationBox仅有的两个值是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服务器日志(在本地主机上运行)

这是日志输出,显示nil值被添加到db中,用于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哈希上使用该名称: params[:suggestion_box]

您的参数将在应有的suggestion_box下进入suggestion_boxsuggestion_box

您的代码使用的是suggestion ,而不是参数,正如您发布的日志所示,通常是Rails假定的那样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM