加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 1错误禁止本书被保存

发布时间:2020-12-17 02:56:59 所属栏目:百科 来源:网络整理
导读:我在创作新书时遇到了问题. 用户已登录,但仍然显示: 1错误禁止本书被保存 用户必须存在 当我编辑我的书它工作正常,但当我尝试创建新书时,它显示.error / 这是图书管理员: class BooksController ApplicationController before_action :find_book,only:[:sh
我在创作新书时遇到了问题.
用户已登录,但仍然显示:

1错误禁止本书被保存

>用户必须存在

当我编辑我的书它工作正常,但当我尝试创建新书时,它显示.error /

这是图书管理员:

class BooksController < ApplicationController
     before_action :find_book,only:[:show,:edit,:update,:destroy,:upvote]
    before_action :authenticate_user!
  def index
    @books = Book.all.order('created_at DESC')
  end


  def show
  end

  def edit
  end

  def update
    if @book.update(book_params)
        redirect_to @book
    else
        render 'edit'
    end
end
def destroy
@book.destroy
redirect_to root_path,notice: "successfully deleted"
end

def new
    @book =Book.new

end

def create
    @book =Book.new(book_params)
    if @book.save
        redirect_to @book
    else
        render 'new'
    end
end

private

def find_book
    @book=Book.find(params[:id])
end

    def book_params
       params.require(:book).permit(:title,:description,:image)
    end
end

我的books.rb是:

class Book < ApplicationRecord
  #Paperclip.options[:command_path] = 'C:Program FilesImageMagick-7.0.5-Q16'
   has_attached_file :image,styles: { }
   do_not_validate_attachment_file_type :image
  #validates_attachment_content_type :image,content_type: /Aimage/.*z/
  belongs_to :user
end

解决方法

您可能有一个belongs_to:user,并且在保存时未在Book上设置用户.

你可以这样做:

belongs_to :user,:optional => true

或者你可以这样做:

@user.books.save

不要这样做

@book = Book.new
@book.save           # No user provided.

并且,如果您正在使用批量分配,请确保您可以将book_params中的:user_id或:user设置为可以在批量分配中提交.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读