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

ruby-on-rails – Paperclip错误:缺少需要的模型attr_accessor

发布时间:2020-12-16 22:42:10 所属栏目:百科 来源:网络整理
导读:然后我想使用Paperclip为每个列表添加照片.我添加了相应的代码到列表show.html.erb,listing.rb模型,listing_controller.rb和_form.html.erb部分. 当我尝试上传图片给列表我得到这个错误: Paperclip::Error in ListingsController#updateListing model missi
然后我想使用Paperclip为每个列表添加照片.我添加了相应的代码到列表show.html.erb,listing.rb模型,listing_controller.rb和_form.html.erb部分.

当我尝试上传图片给列表我得到这个错误:

Paperclip::Error in ListingsController#update
Listing model missing required attr_accessor for 'avatar_file_name'

listing_controller的第44行:

def update
 respond_to do |format|
  if @listing.update(listing_params)
    format.html { redirect_to @listing,notice: 'Listing was successfully updated.' }
    format.json { head :no_content }
  else

需要尝试的几件事情:即将一些代码添加到listing.rb模型中,以使可接受的图像为:头像更加健壮.这是几个stackoverflow帖子提到添加到listing.rb模型:

validates_attachment_content_type :avatar,:content_type => %w(image/jpeg image/jpg image/png)

不幸的是,当我附加图像时,我仍然会收到相同的错误.当我没有附加图像时,我的默认图像加载正常,并且列表正确创建.

我的房源编号:

class Listing < ActiveRecord::Base
  has_attached_file :avatar,:styles => { :medium => "150x",:thumb => "100x100>" },:default_url => "default.jpg"
  validates_attachment_content_type :avatar,:content_type => %w(image/jpeg image/jpg image/png) 
end

我的_form.html.erb部分:

<%= form_for @listing,:html => { :multipart => true } do |f| %>
  <% if @listing.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@listing.errors.count,"error") %> prohibited this listing from being saved:</h2>

      <ul>
      <% @listing.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="form-group">
    <%= f.label :name %><br>
    <%= f.text_field :name,class: "form-control" %>
  </div>
  <div class="form-group">
    <%= f.label :company %><br>
    <%= f.text_field :company,class: "form-control" %>
  </div>
  <div class="form-group">
    <%= f.label :email %><br>
    <%= f.text_field :email,class: "form-control" %>
  </div>
  <div class="form-group">
    <%= f.label :phone %><br>
    <%= f.text_field :phone,class: "form-control" %>
  </div>
  <div class="form-group">
    <%= f.label :avatar %><br>
    <%= f.file_field :avatar,class: "form-control" %>
  </div>
  <div class="form-group">
    <%= f.submit class: "btn btn-primary" %>
  </div>
<% end %>

我的listing_controller.rb控制器:

def update
    respond_to do |format|
      if @listing.update(listing_params)
        format.html { redirect_to @listing,notice: 'Listing was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @listing.errors,status: :unprocessable_entity }
      end
    end
  end
...
def listing_params
   params.require(:listing).permit(:name,:company,:email,:phone,:avatar)
end

和我的schema.rb文件

ActiveRecord::Schema.define(version: 20140329174335) do

  create_table "listings",force: true do |t|
    t.string   "name"
    t.string   "company"
    t.string   "email"
    t.string   "phone"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

编辑:在运行$rails之后添加控制台输出生成纸夹列出头像

(我需要10个信誉点放在后,所以你必须解决链接http://i.imgur.com/c8KGTa3.png)

解决方法

我想你忘了在列表表中创建头像的相应字段.

我建议您生成迁移以将头像添加到列表表中,如下所示:

rails generate paperclip listing avatar

然后运行rake db:migrate

UPDATE

根据您的评论和编辑,您有一个迁移文件添加头像到您创建的列表表,您通过运行rails生成paperclip用户头像,但不幸的是由于某种原因,它不经过ie,没有头像特定的字段(“avatar_file_name”,根据您的db / schema.rb,列表表中的“avatar_content_type”,“avatar_file_size”和“avatar_updated_at”).这是一个非常奇怪的行为.

我建议你按照以下步骤按顺序:

破坏现有迁移(如果有):

rails destroy paperclip listing avatar

生成新的迁移:

rails generate paperclip listing avatar

rake db:migrate

更新2

我希望你没有投票给我(但有人做了),所以我想提醒一下,这是PaperClip的一个持续的问题,我在我的评论(3月31日)中提出了一个解决方案如下:

I want you to try as gem ‘paperclip’,:git =>
“git://github.com/thoughtbot/paperclip.git” in Gemfile and then bundle
install. Let me know when you finish

显然,没有注意到你或某人谁今天投票给我.
另外,你说没有错误,据我所知,图像在这里:i.imgur.com/c8KGTa3.png但是如果你看输出有一个错误说明清楚:

migration_file_name’: protected methodmigration_file_name’ called for PaperclipGenerator:0x007fb3c6494c20 (NoMethodError)

(编辑:李大同)

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

    推荐文章
      热点阅读