简体   繁体   English

RoR未定义方法'attr_accessor'

[英]RoR Undefined Method 'attr_accessor'

I am building a blog that allows me to post pictures, and when a new post w/ a picture comes up, I get an error saying "require attr_accessor for 'image_file_name'". 我正在建立一个博客,使我可以发布图片,并且当出现带有图片的新帖子时,我收到一条错误消息,提示“要求'image_file_name'的attr_accessor'”。 So I put my "attr_accessor :image" in there and I get an "undefined method error 'attr_accessor'". 因此,我将“ attr_accessor:image”放入其中,并得到“未定义的方法错误'attr_accessor'”。 https://github.com/BBaughn1/savagelysaving <-- the github if you need it https://github.com/BBaughn1/savagelysaving <-如果需要的话

def create
    @post = Post.new(post_params)
    if @post.save
      redirect_to @post
    else
      render 'new'
    end
  end

This is before the error, 这是错误之前

def create
    attr_accessor :image
    @post = Post.new(post_params)
    if @post.save
      redirect_to @post
    else
      render 'new'
    end
  end

and this is after the error. 这是错误之后。 And, I got this error earlier but fixed it with this: 而且,我较早收到此错误,但已通过以下方法修复了该错误:

def update
    @post = Post.find(params[:id])
    attr_accessor :image
    if @post.update(params[:post].permit(:title, :body, :image))
      redirect_to @post
    else
      render 'edit'
    end
  end

putting "attr_accessor :image" before the "@post = Post.new(post_params)" will give me the same error saying that I require "attr_accessor" 将“ attr_accessor:image”放在“ @post = Post.new(post_params)”之前会给我同样的错误,提示我需要“ attr_accessor”

It looks like this is simply a naming mismatch. 看起来这只是一个命名不匹配。 Your schema shows you have the attributes avatar_file_name, avatar_content_type, avatar_file_size, and avatar_updated_at. 您的架构显示您具有avatar_file_name,avatar_content_type,avatar_file_size和avatar_updated_at属性。 But in your model , you have has_attached_file :image . 但是在您的模型中 ,您具有has_attached_file :image These two items should match up . 这两个项目应该匹配 it sounds like you probably want has_attached_file :avatar , instead. 听起来您可能想要has_attached_file :avatar来代替。 You will need to replace other relevant occurrences of "image" with "avatar" in your views and controllers, as well. 您还需要在视图和控制器中将其他相关的“图像”出现替换为“头像”。 You also should remove the attr_accessor :image from your model. 您还应该从模型中删除attr_accessor :image Your error indicates that the required fields do not exist in the database, for the structure you have defined. 您的错误表明对于您定义的结构,数据库中不存在必填字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM