简体   繁体   中英

Rails form params not being saved to database

Rails newbie question...

I have a form that is submitting without errors but the form params are not being saved to the database. However, adding the data via rails console works fine. Currently, when the form is submitted, the data is entered as nil into the db.

Any feedback as to why this is happening would be very much appreciated. Also, pls note that the only two values I'm concerned with at this point for SuggestionBox are name and short_name.

Thx in advance for any help!

Model

*Note: the only values I am concerned with at this point are name and 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

(I've only included the new and create actions)

          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

View

This is the _form.html.erb file being used within new.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 Server Log (running on localhost)

This is the log output, showing nil values being added to the db for name and 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)

As you can see on your logs, the params hash comes like this:

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

And in your create action, you got this:

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

But you should use the name on your params hash: params[:suggestion_box]

Your params come in as they should, under the key suggestion_box .

Your code uses suggestion , which is not what the params are, as shown by the log you post, and conventionally assumed, by Rails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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