Rails 4:数据库条目为空并且存在

时间:2014-07-06 03:34:10

标签: postgresql post ruby-on-rails-4 commit

首先,问题:

Started POST "/weapons" for 127.0.0.1 at 2014-07-06 00:13:15 +0200
Processing by WeaponsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Token-Raus-editiert", "art"=>"Schwert", "name"=>"Excalibur", "angriff"=>{"{:in=>1..6, :step=>1}"=>"5"}, "block"=>{"{:in=>1..6, :step=>1}"=>"2"}, "zustand"=>{"{:in=>1..100, :step=>1}"=>"100"}, "button"=>""}
   (0.2ms)  BEGIN
  Weapon Exists (0.4ms)  SELECT  1 AS one FROM "weapons"  WHERE "weapons"."name" IS NULL LIMIT 1
  Weapon Exists (0.8ms)  SELECT  1 AS one FROM "weapons"  WHERE "weapons"."id" IS NULL LIMIT 1
   (0.2ms)  ROLLBACK
Redirected to http://localhost:3000/weapons/new
Completed 302 Found in 13ms (ActiveRecord: 1.6ms)
他告诉我,我试图创造的武器已经存在。在他的逻辑中,什么是正确的。因为,他试图提交“Nothing”而不是ID和creation_date。所以,我的数据库中只有一个空字段。

这是发送请求的表单:

<%= form_for @weapon  do %>
    <div class="WeaponGenerate">
      <table>
        <th><h1>Eine neue Waffe erschaffen</h1></th>
        <tr>
          <td>Welche Sorte Waffe soll es sein?</td>
          <td><%= select_tag :art, options_for_select(['Schwert', 'Axt', 'Bogen']) %></td>
        </tr>
        <tr>
          <td>Der Name für die Waffe soll sein?</td>
          <td><%= text_field_tag :name %></td>
        </tr>
        <tr>
          <td>Angriffswert</td>
          <td><%= number_field(:angriff,  in: 1..6, step: 1) %></td>
        </tr>
        <tr>
          <td>Blockwert</td>
          <td><%= number_field(:block,  in: 1..6, step: 1) %></td>
        </tr>
        <tr>
          <td>Zustand?</td>
          <td><%= number_field(:zustand,  in: 1..100, step: 1) %></td>
        </tr>
        <tr>
          <td></td>
          <td><%= button_tag('Waffe erschaffen') %></td>
      </table>
    </div>

<% end %>

这个控制器:

class WeaponsController < ApplicationController

  def index
    @weapon = Weapon.all
  end

  def new
    @weapon = Weapon.new
  end    

  def create
    @weapon = Weapon.new(weapon_params)
    if @weapon.save
      flash[:notice] = "Schwert erschaffen!"
      redirect_to(:action => 'show')
    else
      flash[:error] = "Leider gab es ein Problem!"
      redirect_to(:action => 'new')
    end
  end

  private
  def weapon_params
    params.require(:weapon).permit(:art, :name, :angriff, :block, :zustand) if params[:weapon]
  end
end

至少是模型,带注释:

# == Schema Information
#
# Table name: weapons
#
#  id         :integer          not null, primary key
#  art        :string(255)
#  name       :string(255)
#  angriff    :integer
#  block      :integer
#  zustand    :float
#  kick       :hstore
#  created_at :datetime
#  updated_at :datetime
#

class Weapon < ActiveRecord::Base
  scope :bySword, lambda { where(:art => 'Schwert' ) }
  scope :byAxe, lambda { where(:art => 'Axt') }
  scope :byBow, lambda { where(:art => 'Bogen') }
  scope :sorted, lambda { order("weapons.art ASC") }
  scope :newest_first, lambda { order("weapons.created_at DESC")}
  scope :search, lambda {|query|
    where(["name LIKE ?", "%#{query}%"])}

    validates_presence_of :art, :name, :angriff, :block, :zustand
    validates_uniqueness_of :name, :id
end

有人看到我的错误吗?我不。所以我希望你能帮助我。

0 个答案:

没有答案