【发布时间】:2020-02-16 18:54:28
【问题描述】:
我是 Rails 新手,在使用我的 Car 模型将新记录插入数据库时遇到问题。
申请如下: 我已经按照 Rails 官方网站的定义构建了文章基础教程应用程序,直到第 5.9 节
我使用设计添加了用户身份验证,并且我想添加一个模型 -cars - 与用户关联。用户拥有_许多汽车,而汽车属于用户。 这是 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
汽车表的结构如下:
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 控制台时,我看到汽车表和用户表已创建,并且创建用户工作正常。
另外,当我尝试创建一辆新车时,我被重定向回我的汽车列表,并且没有加载任何内容,当我检查控制台时,我发现汽车表是空的。
汽车控制器是:
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
汽车/新视图是:
<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 %>
当我发布新车或进入索引页面时,没有错误,并且文章部分中的相同结构(我使用 rails 官方教程创建)工作正常。
当我导航到路径/汽车或提交新车时,我看到一张空桌子。 当我尝试使用 show 时,通过导航到 cars/:id,出现错误,即不存在具有此 id 的汽车 - 另一个证明没有插入记录。
我对 Rails 很陌生,所以如果没有足够的信息,我很抱歉/我分享了很多信息。如果需要更多信息,请告诉我。
我看过大多数类似主题的问题,但没有找到任何相关的答案。我看到了这个,除其他外:
Rails 4 not inserting values into database
Ruby on Rails not adding record to database
Rails 5: ActiveRecord not creating record
这是创建汽车的轨道日志
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 日志。日志可以在
log/development.log找到。 -
@sureshprasanna70 这是相关部分吗?开始 POST "/cars" for ::1 at 2019-10-20 16:46:15 +0300 Processing by CarsController#create as HTML Parameters: {"authenticity_token"=>"MZYIt+YOELCPSFbZZmammwr7LrdMWQfzlge3k/h8UftwBjqgzHHH9VqHBFHAiwXi2Ej2Y4jM2xhC// "car"=>{"title"=>"car3", "description"=>"123123", "img"=>"123"}, "commit"=>"Save Car"} 重定向到localhost:3000/cars 完成302 在 5 毫秒内找到(ActiveRecord:0.0 毫秒 | 分配:1322
标签: ruby-on-rails ruby activemodel