ActiveRecord::UnknownAttributeError 在 PicturesController#create
ActiveRecord::UnknownAttributeError in PicturesController#create
我正在尝试实现图片上传功能(没有 gem),当我在选择照片后按下提交时出现此错误:
Ac
tiveRecord::UnknownAttributeError in PicturesController#create
unknown attribute 'picture' for Picture.
Extracted source (around line #13):
def create
# make a new picture with what picture_params returns (which is a method we're calling)
**@picture = Picture.new(picture_params)** << where i'm getting the error
if @picture.save
# if the save for the picture was successful, go to index.html.erb
redirect_to pictures_url
如何设置我的环境以便将我的照片保存在数据库中?
控制器:
class PicturesController < ApplicationController
def index
@pictures = Picture.all
end
def new
@picture = Picture.new
end
def create
# make a new picture with what picture_params returns (which is a method we're calling)
@picture = Picture.new(picture_params)
if @picture.save
# if the save for the picture was successful, go to index.html.erb
redirect_to pictures_url
else
# otherwise render the view associated with the action :new (i.e. new.html.erb)
render :new
end
end
def show
@picture = Picture.find(params[:id])
end
def edit
@picture = Picture.find(params[:id])
end
def update
@picture = Picture.find(params[:id])
if @picture.update_attributes(picture_params)
redirect_to "/pictures/#{@picture.id}"
else
render :edit
end
end
def destroy
@picture = Picture.find(params[:id])
@picture.destroy
redirect_to pictures_url
end
private
def picture_params
params.require(:picture).permit(:artist, :title, :url, :picture)
end
end
迁移:
class CreatePictures < ActiveRecord::Migration
def change
create_table :pictures do |t|
t.string :artist
t.string :title
t.string :url
t.string :pictures
t.timestamps null: false
end
end
end
我通过编辑文件手动添加了 t.string :pictures,它仍然可以这样工作还是有我需要的命令 运行?
我的表格:
<container>
<center>
<%= form_for @picture do |f| %>
<input type="file" multiple> <%= f.file_field :picture %>
<p>Drag your files here or click in this area.</p>
<button type="submit"> <%= f.submit "Save" %> Upload </button>
</form>
<% end %>
</container>
我正在使用简单的拖放上传。
谢谢你的帮助,我真的很感激!
ActiveRecord::UnknownAttributeError in PicturesController#create
unknown attribute 'picture' for Picture
您的 迁移文件 中有 :pictures
而不是 :picture
,并且您正在使用 :picture
在 picture_params
方法中。
执行rake db:rollback
,将其更改为:picture
并执行rake db:migrate
我正在尝试实现图片上传功能(没有 gem),当我在选择照片后按下提交时出现此错误:
Ac
tiveRecord::UnknownAttributeError in PicturesController#create
unknown attribute 'picture' for Picture.
Extracted source (around line #13):
def create
# make a new picture with what picture_params returns (which is a method we're calling)
**@picture = Picture.new(picture_params)** << where i'm getting the error
if @picture.save
# if the save for the picture was successful, go to index.html.erb
redirect_to pictures_url
如何设置我的环境以便将我的照片保存在数据库中?
控制器:
class PicturesController < ApplicationController
def index
@pictures = Picture.all
end
def new
@picture = Picture.new
end
def create
# make a new picture with what picture_params returns (which is a method we're calling)
@picture = Picture.new(picture_params)
if @picture.save
# if the save for the picture was successful, go to index.html.erb
redirect_to pictures_url
else
# otherwise render the view associated with the action :new (i.e. new.html.erb)
render :new
end
end
def show
@picture = Picture.find(params[:id])
end
def edit
@picture = Picture.find(params[:id])
end
def update
@picture = Picture.find(params[:id])
if @picture.update_attributes(picture_params)
redirect_to "/pictures/#{@picture.id}"
else
render :edit
end
end
def destroy
@picture = Picture.find(params[:id])
@picture.destroy
redirect_to pictures_url
end
private
def picture_params
params.require(:picture).permit(:artist, :title, :url, :picture)
end
end
迁移:
class CreatePictures < ActiveRecord::Migration
def change
create_table :pictures do |t|
t.string :artist
t.string :title
t.string :url
t.string :pictures
t.timestamps null: false
end
end
end
我通过编辑文件手动添加了 t.string :pictures,它仍然可以这样工作还是有我需要的命令 运行?
我的表格:
<container>
<center>
<%= form_for @picture do |f| %>
<input type="file" multiple> <%= f.file_field :picture %>
<p>Drag your files here or click in this area.</p>
<button type="submit"> <%= f.submit "Save" %> Upload </button>
</form>
<% end %>
</container>
我正在使用简单的拖放上传。 谢谢你的帮助,我真的很感激!
ActiveRecord::UnknownAttributeError in PicturesController#create
unknown attribute 'picture' for Picture
您的 迁移文件 中有 :pictures
而不是 :picture
,并且您正在使用 :picture
在 picture_params
方法中。
执行rake db:rollback
,将其更改为:picture
并执行rake db:migrate