如何在论坛应用程序中的回复表格上插入图片字段?

时间:2019-07-04 10:33:29

标签: image ruby-on-rails-5 carrierwave

我尝试在我的新应用类型论坛中的回复表单上添加图片字段。 该应用程序包含主体sujet,并且所有连接的用户都可以对replie部分进行答复。 现在,我尝试将图像字段添加到较旧的文本字段。 我使用载波来实现该功能。

但是我在终端收到此错误消息:

    NoMethodError - undefined method `reimage_will_change!' for # 
 <Reply:0x000055cb9f2abc30>
Did you mean?  Reimage_will_change!:
  app/controllers/replies_controller.rb:8:in `create'

为什么主观注意事项:reimage_will_change:

我使用“重新成像”该变量字段。

请参阅完整的消息术语:

     Started POST "/discussions/pourquoi-intel-a-t-il-du-mal-a-suivre-la-loi-de-moore-la-loi-de-moore-est-elle-morte/replies" for ::1 at 2019-07-04 10:12:20 +0000
       (1.5ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
      ↳ /home/chatln/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
    Processing by RepliesController#create as JS
      Parameters: {"utf8"=>"✓", "reply"=>{"reply"=>"Amazon CEO Jeff Bezos gave this advice to those ", "reimage"=>#<ActionDispatch::Http::UploadedFile:0x000055d560cc1150 @tempfile=#<Tempfile:/tmp/RackMultipart20190704-14120-1uwpx6n.png>, @original_filename="Capture d’écran de 2019-06-10 11-08-07.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"reply[reimage]\"; filename=\"Capture d\xE2\x80\x99\xC3\xA9cran de 2019-06-10 11-08-07.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Envoyer", "discussion_id"=>"pourquoi-intel-a-t-il-du-mal-a-suivre-la-loi-de-moore-la-loi-de-moore-est-elle-morte"}
      User Load (1.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
      ↳ /home/chatln/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
      Discussion Load (0.8ms)  SELECT  "discussions".* FROM "discussions" WHERE "discussions"."slug" = $1 LIMIT $2  [["slug", "pourquoi-intel-a-t-il-du-mal-a-suivre-la-loi-de-moore-la-loi-de-moore-est-elle-morte"], ["LIMIT", 1]]
####
      ↳ app/controllers/replies_controller.rb:55
    Completed 500 Internal Server Error in 225ms (ActiveRecord: 24.9ms)
####  

    NoMethodError - undefined method `reimage_will_change!' for #<Reply:0x00007ffb6c716fa0>
    Did you mean?  Reimage_will_change!:
      app/controllers/replies_controller.rb:8:in `create'

我认为错误来自于set_discussion级别的回复控制器的第55行。

 def set_discussion
    @discussion = Discussion.friendly.find(params[:discussion_id])
 end

和replies_controller完成

 class RepliesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_reply, only: [:edit, :update, :show, :destroy]
  before_action :set_discussion, only: [:create, :edit, :show, :update, :destroy]
  #before_action :find_discussions, only: [:create, :edit, :show, :update, :destroy]

  def create
    @reply = @discussion.replies.create(params[:reply].permit(:reply, :reimage, :discussion_id))
    @reply.user_id = current_user.id

    respond_to do |format|
      if @reply.save
        format.html { redirect_to discussion_path(@discussion) }
        format.js # renders create.js.erb
      else
        format.html { redirect_to discussion_path(@discussion), notice: "Reponse non enregistrée, ressayer encore."}
        format.js
      end
    end
  end

  def new
  end


  def destroy
    @reply = @discussion.replies.find(params[:id])
    @reply.destroy
    redirect_to discussion_path(@discussion)
  end

  def edit
    @discussion = Discussion.find(params[:discussion_id])
    @reply = @discussion.replies.find(params[:id])
  end

  def update
    @reply = @discussion.replies.find(params[:id])
     respond_to do |format|
      if @reply.update(reply_params)
        format.html { redirect_to discussion_path(@discussion), notice: 'Reponse mise a jour...' }
      else
        format.html { render :edit }
        format.json { render json: @reply.errors, status: :unprocessable_entity }
      end
    end
  end

  def show
  end

  private

  def set_discussion
    @discussion = Discussion.friendly.find(params[:discussion_id])
  end


  def set_reply
    @reply = Reply.find(params[:id])
  end

  def reply_params
    params.require(:reply).permit(:reply, :reimage, :discussion_id)
  end
end

和模型回复

  class Reply < ApplicationRecord
    mount_uploader :reimage, ReimageUploader

      belongs_to :discussion
      belongs_to :user
      validates :reply, presence: true

      extend FriendlyId
      friendly_id :reply, use: [:slugged, :finders]

      def should_generate_new_friendly_id?
        reply_changed?
      end    
    end

1 个答案:

答案 0 :(得分:0)

未定义的方法“ x_will_change!”如果您忘记在模型的数据库表中添加列,则会发生#。如果您有模型答复和ReimageUploader,并且已按照Carrierwave文档中的说明安装了上传器:

class Reply < ActiveRecord::Base
  mount_uploader :reimage, ReimageUploader
end

然后错误将显示

未定义的方法“ reimage_will_change!”对于# 要解决此问题,请在控制台中添加一列,在控制台中运行以下命令:

rails g migration AddReimageToUsers reimage:string

这将产生以下迁移:

class AddReimageToUsers < ActiveRecord::Migration
  def change
  add_column :replies, :reimage, :string
  end
end

迁移它以应用更改(在控制台中写下以下命令):

rake db:migrate