Ruby on rails Post model缺少';图像文件名';
我刚开始用RubyonRails做一个示例网页。 当我试图从表单发布图像时,收到一个错误“发布模型缺少‘图像文件名称’所需的属性访问器” 我的代码是 routes.rbRuby on rails Post model缺少';图像文件名';,ruby-on-rails,ruby,Ruby On Rails,Ruby,我刚开始用RubyonRails做一个示例网页。 当我试图从表单发布图像时,收到一个错误“发布模型缺少‘图像文件名称’所需的属性访问器” 我的代码是 routes.rb Rails.application.routes.draw do resources :posts root 'posts#index' end posts_controll.rb class PostsController < ApplicationController def index e
Rails.application.routes.draw do
resources :posts
root 'posts#index'
end
posts_controll.rb
class PostsController < ApplicationController
def index
end
def new
@post = Post.new
end
def create
@post = Post.create(post_params)
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:image, :caption)
end
end
class PostsController
newhtml.erb
<%= simple_form_for @post do |f| %>
<%= f.input :image %>
<%= f.input :caption %>
<%= f.button :submit %>
<% end %>
post.rb
class Post < ActiveRecord::Base
validates :image, presence: true
has_attached_file :image, styles: { :medium => "640x" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
class Post“640x”}
验证\u附件\u内容\u类型:图像,:内容\u类型=>/\Aimage\/.\Z/
结束
我不明白这里的错误。我想我做得很好
伙计们,请帮帮我。错误很清楚,但您的代码不清楚。“image\u file\u name”这是从哪里来的?您的images表根本没有名为image\u file\u name的字段。如果它有这样一个字段,那么activerecord将为您创建attr_访问器。您应该考虑通过迁移添加它。错误是清楚的,但是您的代码不是。“image\u file\u name”这是从哪里来的?您的images表根本没有名为image\u file\u name的字段。如果它有这样一个字段,那么activerecord将为您创建attr_访问器。您应该考虑通过迁移添加它。