上传图片时,ActionController会抛出UnknownFormat

时间:2018-01-12 01:06:50

标签: ruby-on-rails ruby

我试图通过我的Rails应用程序上传照片,一切正常,直到我点击"更新..."。然后它会抛出"ActionController::UnknownFormat"错误并失败。在这里,您可以找到我使用的表格,更新控制器和我所指的模型。

形式:

<%= form_with(model: current_user, local: true, html: {multipart: true}) do |form| %>
                          <div class="field">
                            <%= form.label :profile_pic %>
                            <%= form.file_field :profile_pic, id: :profile_pic %>
                          </div>
                          <div class="actions">
                            <%= form.submit %>
                          </div>
                       <% end %>

更新方法:

  def update
    @user = current_user
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        render action: :edit
      end
    end
  end

错误似乎在这里:

respond_to do |format|

模型:

require 'digest'
class User < ApplicationRecord
  mount_uploader :profile_pic, ProfilePicUploader
    attr_accessor :password
  before_save :encrypt_new_password
    after_create :build_profile 
    has_one :profile
    has_many :trips
    has_many :comments, through: :trips, source: :comments
    has_many :posts, through: :trips, source: :posts

    scope :recent_comments, ->{where("comments.created_at > ? AND user_id = ?", [6.months.ago, self.id]).limit(3)}
    #friends
    has_many :users

    validates :email, uniqueness: {case_sensitive: false, message: 'El correo debe ser único'}, length: {in: 6..50, too_short: "debe tener al menos %{count} caracteres"}, format: {multiline: true,with: /^.+@.+$/, message: "formato de correo no valido"}

    validates :password, confirmation: true, length: {within: 4..20}, presence: {if: :password_required?}
    validates :password_confirmation, presence: true

  def self.authenticate(email,password)
    user = find_by_email(email)
    return user if user && user.authenticated?(password)
  end

  def authenticated?(password)
    self.hashed_password == encrypt(password)
  end

  protected
  def encrypt_new_password
    return if password.blank?
    self.hashed_password = encrypt(password)
  end
  def password_required?
    hashed_password.blank? || password.present?
  end
  def encrypt(string)
    Digest::SHA1.hexdigest(string) 
  end
  def build_profile
    Profile.create(user: self, name: self.name, bio:"Im using Tripper!")  
  end
end

如果有人能告诉我,那我做错了什么......

2 个答案:

答案 0 :(得分:0)

通过更新RichEditBox操作

尝试以下操作
{{1}}

希望能提供帮助

答案 1 :(得分:0)

您已使用respond_to块,但未指定应呈现edit操作的格式。

尝试更新update操作,如下所示:

def update
  @user = current_user
  respond_to do |format|
    if @user.update(user_params)
      format.html { redirect_to @user, notice: 'User was successfully updated.' }
      format.json { render :show, status: :ok, location: @user }
    else
      format.html { render 'edit'} # Specify the format in which you are rendering the 'edit' page
      format.json { render json: @user.errors } # If required, specify a json format as well
    end
  end
end