图像名称未插入Rails中的ruby数据库中 [英] Image name not inserted into database in ruby on rails

查看:67
本文介绍了图像名称未插入Rails中的ruby数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将图像名称存储到数据库时遇到问题.图像上传到文件夹可以正常工作,但是图像名称不会保存到数据库中

I have a problem to store image name into database. image upload to folder working fine but image name will not saved into db

型号代码:

 class Post < ActiveRecord::Base
   attr_accessible :name, :imag
   attr_accessor :imag

  def self.save(upload)
    name = upload['imag'].original_filename
    directory = 'public/data'
    # render :text => directory
    # create the file path
    path = File.join(directory,name)
    # write the file
    File.open(path, "wb") { |f| f.write(upload['imag'].read)}
  end

end

控制器代码:

  def create
    @a=params[:post][:imag].original_filename  /* how to pass in this image name into params[:post] */
    pos= Post.save(params[:post])
    if pos
      redirect_to :action =>"index"
    else
      redirect_to :action =>"posts"
    end
  end

任何人都指导我将其存档.预先感谢.

Anyone guide me to archive this one. Thanks in advance.

推荐答案

我找到了解决方案.在Post.create函数中手动添加图像原始文件名.

I have find the solution. Manually added image original file name in Post.create function.

控制器代码:

 def create
            Post.super(params[:post])
            pos= Post.create(:name=>params[:post][:name],:image=>params[:post][:image].original_filename) /* here is added image value from uploaded input */
            if pos
              redirect_to :action =>"index"
            else
              redirect_to :action =>"posts"
            end

          end

型号代码:

class Post < ActiveRecord::Base
   attr_accessible :name, :image

  #attr_accessor :imag
  def self.super(upload)
    name = upload['image'].original_filename
    directory = 'public/data'
   # render :text => directory
    # create the file path
    path = File.join(directory,name)
    # write the file
    File.open(path, "wb") { |f| f.write(upload['image'].read)}
  end

end

这篇关于图像名称未插入Rails中的ruby数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆