我目前正在构建一个我有书模型的应用程序。
它在本地工作,没有任何问题。我将它部署到Heroku并运行rake db:migrate
并尝试创建一本书
我遇到了ROLLBACK错误。
2018-03-29T07:29:59.748305+00:00 app[web.1]: D, [2018-03-29T07:29:59.748238 #4] DEBUG -- : [feda9269-2f19-4916-a87f-bc179fd52bec] User Load (1.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 3], ["LIMIT", 1]]
2018-03-29T07:29:59.750826+00:00 app[web.1]: D, [2018-03-29T07:29:59.750763 #4] DEBUG -- : [feda9269-2f19-4916-a87f-bc179fd52bec] (0.8ms) BEGIN
2018-03-29T07:29:59.763631+00:00 app[web.1]: D, [2018-03-29T07:29:59.763526 #4] DEBUG -- : [feda9269-2f19-4916-a87f-bc179fd52bec] (0.9ms) ROLLBACK
2018-03-29T07:29:59.764270+00:00 app[web.1]: I, [2018-03-29T07:29:59.764208 #4] INFO -- : [feda9269-2f19-4916-a87f-bc179fd52bec] Rendering books/new.html.erb within layouts/application
2018-03-29T07:29:59.770061+00:00 app[web.1]: I, [2018-03-29T07:29:59.769996 #4] INFO -- : [feda9269-2f19-4916-a87f-bc179fd52bec] Rendered books/_form.html.erb (5.5ms)
2018-03-29T07:29:59.770167+00:00 app[web.1]: I, [2018-03-29T07:29:59.770113 #4] INFO -- : [feda9269-2f19-4916-a87f-bc179fd52bec] Rendered books/new.html.erb within layouts/application (5.8ms)
2018-03-29T07:29:59.771544+00:00 app[web.1]: I, [2018-03-29T07:29:59.771486 #4] INFO -- : [feda9269-2f19-4916-a87f-bc179fd52bec] Rendered layouts/_navbar.html.erb (0.7ms)
2018-03-29T07:29:59.771904+00:00 app[web.1]: I, [2018-03-29T07:29:59.771828 #4] INFO -- : [feda9269-2f19-4916-a87f-bc179fd52bec] Rendered layouts/_alerts.html.erb (0.2ms)
2018-03-29T07:29:59.772181+00:00 app[web.1]: I, [2018-03-29T07:29:59.772125 #4] INFO -- : [feda9269-2f19-4916-a87f-bc179fd52bec] Completed 200 OK in 28ms (Views: 8.2ms | ActiveRecord: 3.4ms)
书籍控制器
class BooksController < ApplicationController
before_action :find_book, only: [:show, :edit, :update, :destroy]
before_action :book_owner, only: [:destroy, :edit, :update]
def index
@books = Book.all.order("created_at DESC")
end
def show
end
def new
@book = current_user.books.build
@categories = Category.all.map{ |c| [c.name, c.id] }
end
def create
@book = current_user.books.build(book_params)
@book.category_id = params[:category_id]
if @book.save
redirect_to books_path
else
render 'new'
end
end
def edit
@categories = Category.all.map{ |c| [c.name, c.id] }
end
def update
@book.category_id = params[:category_id]
if @book.update(book_params)
redirect_to book_path(@book)
else
render 'edit'
end
end
def destroy
@book.destroy
redirect_to root_path
end
private
def book_params
params.require(:book).permit(:title, :description, :author, :category_id)
end
def find_book
@book = Book.find(params[:id])
end
def book_owner
unless current_user.id == @book.user_id
flash[:notice] = "You are not allowed to do that!"
redirect_to @book
end
end
end
预订模型
class Book < ApplicationRecord
belongs_to :user
belongs_to :category
end
预订新视图
<%= simple_form_for @book, :html => { :multipart => true } do |f| %>
<%= f.input :category_id, collection: @categories, prompt: "Select a category" %>
<%= f.input :title, label: "Book Title" %>
<%= f.input :author %>
<%= f.input :description %>
<%= f.button :submit, :class => "btn-outline-primary" %>
<% end %>
更新
Started POST "/books" for 127.0.0.1 at 2018-03-29 11:13:22 +0200
Processing by BooksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"UYKPhHA99zg5TMMwgKGk+h2OPPaCQkpkjohPRnRNQHlJB1eQF4Ooro4Tvx9VDSr3U6/H4AGoThu8jzkrwyMbUB==", "book"=>{"category_id"=>"", "title"=>"tesas", "author"=>"asdasd", "description"=>"asdasd"}, "commit"=>"Create Book"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.1ms) BEGIN
(0.3ms) ROLLBACK
Rendering books/new.html.erb within layouts/application
Rendered books/_form.html.erb (10.0ms)
Rendered books/new.html.erb within layouts/application (11.9ms)
Rendered layouts/_navbar.html.erb (1.2ms)
Rendered layouts/_alerts.html.erb (0.3ms)
Completed 200 OK in 52ms (Views: 46.3ms | ActiveRecord: 0.7ms)
Category_id是零,因为我还没有将它们设置在我的第二台笔记本电脑上,因为我无法使用Category.crection(Category.create(名称:&#34; SOMECATEGORY&#34;))这可以创建一个类别,但没有Category.connection它就不会工作 在我的heroku应用程序中,我能够进行Category.connection和Category.create .. - 但即使在那之后它也无法工作。
我做错了什么?一如既往地感谢您的帮助!
答案 0 :(得分:0)
您收到错误是因为记录无效。原因是您未正确分配category_id
。
您需要将params[:category_id]
更改为book_params[:category_id]
params [:category_id]将为您提供nil
,验证检查将失败。
def create
@book = current_user.books.build(book_params)
@book.category_id = book_params[:category_id]
if @book.save
redirect_to books_path
else
render 'new'
end
end
您需要对update
操作进行相同的更改
答案 1 :(得分:0)
更新
我想我发现了我的错误:我的user_id在通过网络创建时为零 - 我在控制台上创建了相同的记录并传递了参数user_id - voila,works。我的user_id参数以某种方式不会传递到heroku rails服务器,但它在本地计算机上传递。没有更改代码行。
这怎么可能?