Pics中的NoMethodError #new

时间:2016-08-26 11:56:20

标签: ruby-on-rails

如果我访问http://localhost:3000/pics/new我收到此错误

显示c:/Sites/insta/app/views/pics/_form.html.haml第12行:

undefined method `image' for #<Pic:0xa2ad520>
Trace of template inclusion: app/views/pics/new.html.haml

Rails.root: c:/Sites/insta

Application Trace | Framework Trace | Full Trace
app/views/pics/_form.html.haml:12:in `block in _app_views_pics__form_html_haml___443805693_84818352'
app/views/pics/_form.html.haml:1:in `_app_views_pics__form_html_haml___443805693_84818352'
app/views/pics/new.html.haml:7:in `_app_views_pics_new_html_haml__186267902_85061856'

my _form.html.haml

= simple_form_for @pic, html: { multipart: true } do |f|
  - if @pic.errors.any?
    #errors
      %h2
      = pluralize(@pic.errors.cont, "error")
      prevented this Pic from saving
      %ul
        - @pic.errors.full_message.each do |msg|
          %li= msg

  .form-group
    = f.input :image, input_html: { class: 'form-control' }
  .form-group
    = f.input :title, input_html: { class: 'form-control' }
  .form-group
    = f.input :description, input_html: { class: 'form-control' }

  = f.button :submit, class: "btn btn-info"

我的new.html.haml

.col-md-8.col-md-offset-2
  .row
    .panel.panel-default
      .panel-heading
        %h1 Post Pic
      .panel-body
        = render 'form'

Pic.rb

class Pic < ActiveRecord::Base
    belongs_to :user
    has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
    validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
end

1 个答案:

答案 0 :(得分:0)

你有一个“头像”,而不是“图像”,所以这应该有用

= f.input :avatar, input_html: { class: 'form-control' }