2

私は Rails の初心者で、最初のアプリに取り組み始めましたが、解決できない問題を抱えています。

基本的に私は賃貸物件を追跡しているので、住宅、テナント、リース、ベンダー、修理などのコントローラーとモデルを持っています。それらすべてをセットアップし、Webフォームから新しいレコードを作成、更新、編集などを行うことができましたテナントと呼ばれるものを除いて。

アプリに Web フォームからレコードを保存できなかったので、Rails コンソールに移動しましたが、同じ問題が発生しました。コンソールからデータベースに保存できません。

新しい「テナント」レコードを作成するためにコンソールで行ったことの例

 tenant = Tenant.new
=> #<Tenant id: nil, first_name: nil, last_name: nil, home_id: nil, created_at: nil, updated_at: nil>
>> first_name = "Billy"
=> "Billy"
>> last_name = "Jones"
=> "Jones"
>> tenant.save
   (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO `tenants` (`created_at`, `first_name`, `home_id`, `last_name`,          `updated_at`) VALUES ('2012-07-29 20:54:28', NULL, NULL, NULL, '2012-07-29 20:54:28')`  
Mysql2::Error: Column 'first_name' cannot be null: INSERT INTO `tenants` (`created_at`, `first_name`, `home_id`, `last_name`, `updated_at`) VALUES ('2012-07-29 20:54:28', NULL, NULL, NULL, '2012-07-29 20:54:28')
   (0.1ms)  ROLLBACK
ActiveRecord::StatementInvalid: Mysql2::Error: Column 'first_name' cannot be null: INSERT INTO `tenants` (`created_at`, `first_name`, `home_id`, `last_name`, `updated_at`) VALUES ('2012-07-29 20:54:28', NULL, NULL, NULL, '2012-07-29 20:54:28')

SO, I cannot determine the problem.  It says first_name cannot be null, obviously because I setup the database that way, but there is a value trying to go into database, even though I can return a value in Rails Console asking for first_name.....  Any advise?


Below are controller/model



***** TENANT CONTROLLER
class TenantsController < ApplicationController

    def index
        render('list')
    end

    def list
      @tenants = Tenant.order("tenants.last_name ASC")
    end

    def show
      @tenant = Tenant.find(params[:id])
    end

    def new
      @tenant = Tenant.new
    end

    def create
        @tenant = Tenant.new(params[:tenant])
        @tenant.save

        if @tenant.save
            flash[:notice] = "New Tenant was created successfully."
            redirect_to(:action => 'list')
        else
            render('new')
        end
    end

    def edit
      @tenant = Tenant.find(params[:id])

    end


    def update
        # find object using form parameters
        @tenant = Tenant.find(params[:id])

        #update the object
        if @tenant.update_attributes(params[:tenant])

            #if update succeeds redirect to
            flash[:notice] = "Tenant was updated successfully."
            redirect_to(:action => 'show', :id => @tenant.id)
            else
            render('edit')
        end

    end

    def delete
        @tenant = Tenant.find(params[:id])

    end

    def destroy
        @tenant = Tenant.find(params[:id])
        @tenant.destroy

        flash[:notice] = "Tenant was destroyed successfully."
        redirect_to(:action => 'list' )

    end


end

***** TENANT MODEL
class Tenant < ActiveRecord::Base
    attr_accessible :first_name, :last_name, :home_id
end
4

2 に答える 2

6

セッションでは、インスタンス化するテナントレコードにconsole割り当てfirst_nameたり、インスタンス化したりすることはありません-もちろん、空です。これを試して;last_nameTenant.new

tenant = Tenant.new
=> #<Tenant id: nil, first_name: nil, last_name: nil, home_id: nil, created_at: nil, updated_at: nil>
>> tenant.first_name = "Billy"
=> "Billy"
>> tenant.last_name = "Jones"
=> "Jones"
>> tenant.save

名前を 1 行で指定することでこれを効率化できcreateます。また、データベースへの即時保存を試みる which を使用することもできます。

tenant = Tenant.create(:first_name => "Billy", :last_name => "Jones")
于 2012-07-29T21:05:03.717 に答える
0

ルビー実行時

first_name = "Billy"

オブジェクトを指定するのを忘れただけで、新しい文字列変数が作成されます。

tenant.first_name = "Billy"

次回、レコードが保存されなかった理由を知るには、次のことを試してください。

puts tenant.errors.full_errors
于 2012-07-29T22:09:07.623 に答える