gpt4 book ai didi

ruby-on-rails - Ruby Rails 数据库

转载 作者:行者123 更新时间:2023-12-04 19:59:10 25 4
gpt4 key购买 nike

我是 Rails 的新手,我已经开始开发我的第一个应用程序,但我遇到了无法解决的问题。

基本上我在跟踪出租属性(property),所以我有房屋、租户、租赁、供应商、维修等的 Controller 和模型。我已经能够设置所有这些并创建新记录、更新、编辑等,来自网络表单,除了一个名为 TENANTS 的表单。

由于我无法从 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

最佳答案

在你的console您永远不会分配的 session first_namelast_name到您通过 Tenant.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

您可以通过在一行中指定名称来简化这一过程,也可以使用 create尝试立即保存到数据库:

tenant = Tenant.create(:first_name => "Billy", :last_name => "Jones")

关于ruby-on-rails - Ruby Rails 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11712901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com