Rails 没有将记录插入数据库
Rails is not inserting recoreds into database
我是 Rails 的新手,我在使用我的汽车模型将新记录插入数据库时遇到了问题。
申请如下:
我已经构建了 Rails 官方网站中定义的文章基础教程应用程序,直到第 5.9 节
我使用 devise 添加了用户身份验证,我想添加一个模型 -cars - 与用户有关联。用户 has_many 辆汽车,一辆汽车 belongs_to 一个用户。
这是 user.rb 模型文件:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :cars
end
cars.rb 文件是:
class Car < ApplicationRecord
belongs_to :user
end
汽车 table 的结构如下:
class CreateCars < ActiveRecord::Migration[6.0]
def change
create_table :cars do |t|
t.string :title
t.text :description
t.string :img
t.timestamps
end
end
end
我添加了用户关联:
class AddUserIdToCars < ActiveRecord::Migration[6.0]
def change
add_column :cars, :user_id, :integer
end
end
现在,当我使用 rails 控制台时,我看到创建了汽车 table 和用户 table,并且创建用户工作正常。
此外,当我尝试创建一辆新车时,我被重定向回我的汽车列表,但没有加载任何内容,当我检查控制台时,我看到汽车 table 是空的。
汽车控制器是:
class CarsController < ApplicationController
def index
@cars = Car.all
end
def new
end
def show
@car = Car.find(params[:id])
end
def create
# render plain: params[:car].inspect
@car = Car.new(car_params)
@car.save
redirect_to @car
end
private
def car_params
params.require(:car).permit(:title, :description, :img)
end
end
cars/new 视图是:
<h1>Add new car</h1>
<%= form_with scope: :car,
url: cars_path,
local: true do |form| %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :description %><br>
<%= form.text_area :description %>
</p>
<p>
<%= form.label :img %><br>
<%= form.text_area :img %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', cars_path %>
汽车索引视图是:
<h1>Listing cars</h1>
<table>
<tr>
<th>Title</th>
<th>description</th>
<th>Img</th>
</tr>
<% @cars.each do |car| %>
<tr>
<td><%= car.title %></td>
<td><%= car.description %></td>
<td><%= car.img %></td>
<td>
<%= link_to 'Show', car_path(car) %>
</td>
</tr>
<% end %>
</table>
<%= link_to 'New Car', new_car_path %>
当我 post 一辆新车或转到索引页面时,没有错误,并且文章部分(我使用 rails 官方教程创建的)中的结构完全相同很好。
当我导航到 path/cars 或提交新车时,我看到空 table。
当我尝试使用 show 时,通过导航到 cars/:id,出现一个错误,即不存在具有此 id 的汽车 - 另一个证明没有插入任何记录。
我是 Rails 的新手,所以很抱歉 info/I 分享的信息不够多。如果需要更多信息,请告诉我。
我看过大多数类似主题的问题,但没有找到相关的答案。我看到了这个,其中包括:
Insert into Rails Database
Ruby on Rails not adding record to database
Rails 5: ActiveRecord not creating record
这是创建汽车的rails日志
Started POST "/cars" for ::1 at 2019-10-20 16:46:15 +0300 Processing by CarsController#create as HTML Parameters: {"authenticity_token"=>"MZYIt+YOELCPSFbZZmammwr7LrdMWQfzlge3k/h8UftwBjqgzHHH9VqHBFHAiwXi2Ej2Y4jM2xhC/V7sil773Q==", "car"=>{"title"=>"car3", "description"=>"123123", "img"=>"123"}, "commit"=>"Save Car"} Redirected to localhost:3000/cars Completed 302 Found in 5ms (ActiveRecord: 0.0ms | Allocations: 1322
为什么有文章却没有插入汽车,即使功能和erb文件几乎相同?
看起来您在 rails 6。因此,默认情况下,当 Car belongs_to :user
时,则需要 user_id
on @car
。但是,在您的 create
操作中,您永远不会在 @car
上设置 user_id
,因此 @car
不会保存。
如果您认为保存应该有效但实际上无效,您可以随时执行 save!
而不是 save
。如果保存不成功,该方法的 banged 版本将引发错误。
无论如何,我认为 CarsController
中的 create
操作应该看起来更像:
class CarsController < ApplicationController
def index
@cars = Car.all
end
def new
@car = Car.new
end
def show
@car = Car.find(params[:id])
end
def create
@car = current_user.cars.new(car_params)
if @car.save
redirect_to @car
else
# do something else
end
end
private
def car_params
params.require(:car).permit(:title, :description, :img)
end
end
通过执行 current_user.cars.new
,user_id
会自动设置在新的 Car
上,您应该可以保存。
回复您在评论中的问题:
Why current_user.cars.new(car_params) and not: current_user.Car.new(car_params)? Why no capital Car was needed, like in Car.find?
Car
是一个 class。 Car.new
在 Car
class 上调用 new
方法。 Car.find
在 Car
class 上调用 find
方法。 Car
不是 current_user
上的方法。因此,current_user.Car
没有意义,应该抛出错误。
cars
(当像 current_user.cars
一样使用时)是 User
的 current_user
实例上的一个方法。 .cars
方法是在您执行 User has_many :cars
时创建的。您可以在 Methods Added by has_many section of the Active Record Associations 指南中看到 has_many
创建的所有方法的列表。
current_user.cars
returns 一个 ActiveRecord_Relation
。当您执行 current_user.cars.new
时,您是在 current_user.cars
返回的 ActiveRecord_Relation
上调用 new
方法。这个 new
方法 returns Car
的一个实例 已经 user_id 设置 ,这是整件事的重点。所以:
Car.new
returns 一个新的 Car
没有 user_id
set,
current_user.cars.new
returns一个新的Car
和user_id
集。
why cars? as I understand, cars is a refrens to the table, and not the car model itself.
没有。如上所述,在这种情况下,cars
是 current_user
上的一种方法,它是在您执行 User has_many :cars
时创建的,并且 returns 代表所有的 ActiveRecord_Relation
Car
个具有 user_id
匹配 current_user.id
.
的实例
我是 Rails 的新手,我在使用我的汽车模型将新记录插入数据库时遇到了问题。
申请如下: 我已经构建了 Rails 官方网站中定义的文章基础教程应用程序,直到第 5.9 节
我使用 devise 添加了用户身份验证,我想添加一个模型 -cars - 与用户有关联。用户 has_many 辆汽车,一辆汽车 belongs_to 一个用户。 这是 user.rb 模型文件:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :cars
end
cars.rb 文件是:
class Car < ApplicationRecord
belongs_to :user
end
汽车 table 的结构如下:
class CreateCars < ActiveRecord::Migration[6.0]
def change
create_table :cars do |t|
t.string :title
t.text :description
t.string :img
t.timestamps
end
end
end
我添加了用户关联:
class AddUserIdToCars < ActiveRecord::Migration[6.0]
def change
add_column :cars, :user_id, :integer
end
end
现在,当我使用 rails 控制台时,我看到创建了汽车 table 和用户 table,并且创建用户工作正常。
此外,当我尝试创建一辆新车时,我被重定向回我的汽车列表,但没有加载任何内容,当我检查控制台时,我看到汽车 table 是空的。
汽车控制器是:
class CarsController < ApplicationController
def index
@cars = Car.all
end
def new
end
def show
@car = Car.find(params[:id])
end
def create
# render plain: params[:car].inspect
@car = Car.new(car_params)
@car.save
redirect_to @car
end
private
def car_params
params.require(:car).permit(:title, :description, :img)
end
end
cars/new 视图是:
<h1>Add new car</h1>
<%= form_with scope: :car,
url: cars_path,
local: true do |form| %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :description %><br>
<%= form.text_area :description %>
</p>
<p>
<%= form.label :img %><br>
<%= form.text_area :img %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', cars_path %>
汽车索引视图是:
<h1>Listing cars</h1>
<table>
<tr>
<th>Title</th>
<th>description</th>
<th>Img</th>
</tr>
<% @cars.each do |car| %>
<tr>
<td><%= car.title %></td>
<td><%= car.description %></td>
<td><%= car.img %></td>
<td>
<%= link_to 'Show', car_path(car) %>
</td>
</tr>
<% end %>
</table>
<%= link_to 'New Car', new_car_path %>
当我 post 一辆新车或转到索引页面时,没有错误,并且文章部分(我使用 rails 官方教程创建的)中的结构完全相同很好。
当我导航到 path/cars 或提交新车时,我看到空 table。 当我尝试使用 show 时,通过导航到 cars/:id,出现一个错误,即不存在具有此 id 的汽车 - 另一个证明没有插入任何记录。
我是 Rails 的新手,所以很抱歉 info/I 分享的信息不够多。如果需要更多信息,请告诉我。
我看过大多数类似主题的问题,但没有找到相关的答案。我看到了这个,其中包括:
Insert into Rails Database
Ruby on Rails not adding record to database
Rails 5: ActiveRecord not creating record
这是创建汽车的rails日志
Started POST "/cars" for ::1 at 2019-10-20 16:46:15 +0300 Processing by CarsController#create as HTML Parameters: {"authenticity_token"=>"MZYIt+YOELCPSFbZZmammwr7LrdMWQfzlge3k/h8UftwBjqgzHHH9VqHBFHAiwXi2Ej2Y4jM2xhC/V7sil773Q==", "car"=>{"title"=>"car3", "description"=>"123123", "img"=>"123"}, "commit"=>"Save Car"} Redirected to localhost:3000/cars Completed 302 Found in 5ms (ActiveRecord: 0.0ms | Allocations: 1322
为什么有文章却没有插入汽车,即使功能和erb文件几乎相同?
看起来您在 rails 6。因此,默认情况下,当 Car belongs_to :user
时,则需要 user_id
on @car
。但是,在您的 create
操作中,您永远不会在 @car
上设置 user_id
,因此 @car
不会保存。
如果您认为保存应该有效但实际上无效,您可以随时执行 save!
而不是 save
。如果保存不成功,该方法的 banged 版本将引发错误。
无论如何,我认为 CarsController
中的 create
操作应该看起来更像:
class CarsController < ApplicationController
def index
@cars = Car.all
end
def new
@car = Car.new
end
def show
@car = Car.find(params[:id])
end
def create
@car = current_user.cars.new(car_params)
if @car.save
redirect_to @car
else
# do something else
end
end
private
def car_params
params.require(:car).permit(:title, :description, :img)
end
end
通过执行 current_user.cars.new
,user_id
会自动设置在新的 Car
上,您应该可以保存。
回复您在评论中的问题:
Why current_user.cars.new(car_params) and not: current_user.Car.new(car_params)? Why no capital Car was needed, like in Car.find?
Car
是一个 class。 Car.new
在 Car
class 上调用 new
方法。 Car.find
在 Car
class 上调用 find
方法。 Car
不是 current_user
上的方法。因此,current_user.Car
没有意义,应该抛出错误。
cars
(当像 current_user.cars
一样使用时)是 User
的 current_user
实例上的一个方法。 .cars
方法是在您执行 User has_many :cars
时创建的。您可以在 Methods Added by has_many section of the Active Record Associations 指南中看到 has_many
创建的所有方法的列表。
current_user.cars
returns 一个 ActiveRecord_Relation
。当您执行 current_user.cars.new
时,您是在 current_user.cars
返回的 ActiveRecord_Relation
上调用 new
方法。这个 new
方法 returns Car
的一个实例 已经 user_id 设置 ,这是整件事的重点。所以:
Car.new
returns 一个新的Car
没有user_id
set,current_user.cars.new
returns一个新的Car
和user_id
集。
why cars? as I understand, cars is a refrens to the table, and not the car model itself.
没有。如上所述,在这种情况下,cars
是 current_user
上的一种方法,它是在您执行 User has_many :cars
时创建的,并且 returns 代表所有的 ActiveRecord_Relation
Car
个具有 user_id
匹配 current_user.id
.