Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从.doc、.docx和rtf文件中获取文本并将其显示到文本字段(Rails)_Ruby On Rails_Forms_Doc - Fatal编程技术网

Ruby on rails 从.doc、.docx和rtf文件中获取文本并将其显示到文本字段(Rails)

Ruby on rails 从.doc、.docx和rtf文件中获取文本并将其显示到文本字段(Rails),ruby-on-rails,forms,doc,Ruby On Rails,Forms,Doc,我有一个Post模型: create_table "posts", :force => true do |t| t.string "title" t.string "content" t.integer "user_id" t.datetime "created_at", :null => false t.datetime "updated_at",

我有一个
Post
模型:

  create_table "posts", :force => true do |t|
    t.string   "title"
    t.string   "content"
    t.integer  "user_id"
    t.datetime "created_at",                            :null => false
    t.datetime "updated_at",                            :null => false
    t.integer  "comments_count",     :default => 0,     :null => false
    t.boolean  "published",          :default => false
    t.datetime "published_at"
    t.boolean  "draft",              :default => false
  end
这就是它的形式:

<%= form_for(@post, :html => { :multipart => true }) do |f| %>
  <%= render 'shared/error_messages' %>
  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %>
    <%= f.text_area :content %>
  </div>

  <div class="field">
    <%= f.label :draft %>
    <%= f.check_box :draft %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
posts\u controller.rb:

class Post < ActiveRecord::Base
  require 'docx'
  .
  .
  .  
  def read_docx
    d = Docx::Document.open(self.document)
    d.each_paragraph do |p|
      puts d
    end
  end
end
class PostsController < ApplicationController
  before_filter :authenticate_user!, :except => [:show, :index]
  .
  .
  .
  def create
    @user = current_user
    @post = @user.posts.new(params[:post])
    @doc_text = (no idea what to do here)

    if @post.save
      redirect_to @post, notice: 'post was successfully created.'
    else
      render action: "new"
    end
  end

  def edit
    @post = Post.find(params[:id])
  end
  .
  .
  .
<%= form_for(@post, :html => { :multipart => true }) do |f| %>
  <%= render 'shared/error_messages' %>
  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %>
    <%= f.text_area :content, :value => @doc_text %>
  </div>
  .
  .
  .
class PostsController[:show,:index]
.
.
.
def创建
@用户=当前用户
@post=@user.posts.new(参数[:post])
@doc_text=(不知道在这里做什么)
如果@post.save
将_重定向到@post,注意:“post已成功创建。”
其他的
渲染操作:“新建”
结束
结束
定义编辑
@post=post.find(参数[:id])
结束
.
.
.
posts/new.html.erb:

class Post < ActiveRecord::Base
  require 'docx'
  .
  .
  .  
  def read_docx
    d = Docx::Document.open(self.document)
    d.each_paragraph do |p|
      puts d
    end
  end
end
class PostsController < ApplicationController
  before_filter :authenticate_user!, :except => [:show, :index]
  .
  .
  .
  def create
    @user = current_user
    @post = @user.posts.new(params[:post])
    @doc_text = (no idea what to do here)

    if @post.save
      redirect_to @post, notice: 'post was successfully created.'
    else
      render action: "new"
    end
  end

  def edit
    @post = Post.find(params[:id])
  end
  .
  .
  .
<%= form_for(@post, :html => { :multipart => true }) do |f| %>
  <%= render 'shared/error_messages' %>
  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %>
    <%= f.text_area :content, :value => @doc_text %>
  </div>
  .
  .
  .
{:multipart=>true})do | f |%>
@文档\文本%>
.
.
.
我已经制作了回形针上传docx文件

我创建了一个名为
:document
的新字段。我刚刚尝试了gem,它运行良好。您可以在其github页面上获得2个示例。遗憾的是,它不适用于文档文件

对于他们,你可以使用这个宝石。github页面上有一些示例,但如果您想获取文档文件的全部内容,请执行以下操作:

require 'msworddoc-extractor'

MSWordDoc::Extractor.load('sample.doc') do |doc|
  puts doc.whole_contents
end
<%= f.text_area :content, :value => @doc_text %> 
#Where @doc_text is the data from file
还有其他方法可以调用
doc
,如
document
header
。再次检查github页面

对于rtf,您也可以这样做

现在,在
内容中传递它很容易。只需定义如何从文件中获取数据,如控制器上调用的外部库:

@doc_text = Parser.doc("file.doc")
@docx_text = Parser.docx("file.docx")
@rtf_text = Parser.rtf("file.rtf")
或者直接或通过任何你想到的方法来获取值。要在视图中显示它,您只需添加如下
:value
选项:

require 'msworddoc-extractor'

MSWordDoc::Extractor.load('sample.doc') do |doc|
  puts doc.whole_contents
end
<%= f.text_area :content, :value => @doc_text %> 
#Where @doc_text is the data from file
@doc\u text%>
#其中@doc_text是文件中的数据

非常感谢!对不起,我是这方面的初学者。我到底要把上面的代码放在哪里?控制器(哪个动作?)或模型或函数?(我只知道在视图中把表单的代码放在哪里)。我现在正在尝试docx。这取决于你到底想要实现什么。您可以在任何地方实现它,但遵循良好的模式,如果您希望在用户访问表单时加载文件,我认为您最好在lib目录中创建一个外部lib,将其包含在控制器上,并调用方法来加载文件。但是如果这听起来很难,那么在控制器的模型中实现方法,并像我一样在控制器中调用它。如果你想尝试gems,我建议你在终端中打开一个
irb
,在那里玩,因为示例既短又简单。对不起,ha是我第一次使用这样的东西。我中途被卡住了(不确定我是否做对了)。请参阅我的编辑。如果您想在用户访问表单时在表单上显示文本,则必须在
new
中创建方法,而不是
create
。您现在应该调用
Post.read_docx
inside
new