upload_progress

ファイルアップロード時にプログレスバーを表示する
ファイルの保存はやってくれないので、自分で書く
rails soft で 標準の scaffold なときの例
/app/controllers/soft_controller.rb
class SoftsController < ApplicationController
  upload_status_for :create

...省略...

  def create
    @soft = Soft.new(params[:soft])
    upload_progress.message = "Processing document..."
    session.update
    set_file_name
    if @soft.save
      save_file
      redirect_to :action => 'show', :id => @soft.id
    else
      render :action => 'new'
    end
  end

  def destroy
    Soft.find(params[:id]).destroy
    delete_file
    redirect_to :action => 'list'
  end

  private

  def set_file_name
    @file = params[:soft][:image]
    @soft.image = @file.original_filename
  end

  def save_file
    if @file and @soft.image.to_s != ""
      @file.binmode
      FileUtils.mkdir_p("#{RAILS_ROOT}/public/files/#{ENV['RAILS_ENV']}/#{@soft.id}")
      if params[:soft][:image].path # file size < 10240 then StringIO. StringIO.path == nil
        File.rename(params[:soft][:image].path, "#{RAILS_ROOT}/public/files/#{ENV['RAILS_ENV']}/#{@soft.id}/#{@soft.image}")
      else
        File.open("#{RAILS_ROOT}/public/files/#{ENV['RAILS_ENV']}/#{@soft.id}/#{@soft.image}", "w"){|f|
          f.binmode
          f.write(@file.read)
        }
      end
    end
  end

  def delete_file
    FileUtils.rm_f("#{RAILS_ROOT}/public/files/#{ENV['RAILS_ENV']}/#{params[:id]}")
  end
app/views/softs/new.rhtml
DB の image というカラムにファイル名が入る
<h1>New soft</h1>

<%= form_tag_with_upload_progress({:action => 'create'}, {}, {:action => :custom_status}) %>
<%= render :partial => 'form' %>
<%= file_field 'soft', 'image' %>
<%= submit_tag "Upload" %><br /><br />
<%= upload_status_tag %>
<%= end_form_tag %>

<%= link_to 'Back', :action => 'list' %>
app/views/softs/list.rhtml
ファイルへのリンクにする場合
<% for soft in @softs %>
  <tr>
  <% for column in Soft.content_columns %>
    <% if column.name == 'image' %>
      <td><%= link_to soft.send(column.name).to_s, '/files/' +  ENV['RAILS_ENV'] + '/' + soft.id.to_s + '/' + soft.send(column.name).to_s %></
td>
    <% else %>
      <td><%=h soft.send(column.name) %></td>
    <% end %>
  <% end %>
    <td><%= link_to 'Show', :action => 'show', :id => soft %></td>
    <td><%= link_to 'Edit', :action => 'edit', :id => soft %></td>
    <td><%= link_to 'Destroy', { :action => 'destroy', :id => soft }, :confirm => 'Are you sure?', :post => true %></td>
  </tr>
<% end %>
</table>

自動でうめこまれる Ajax をながめる用に改行してみたもの
<form action="/hoge/create" enctype="multipart/form-data" method="post" onsubmit="
if (this.action.indexOf('upload_id') &lt; 0) {
  this.action += '?upload_id=8';
}
this.target = 'UploadTarget8';
$('UploadStatus8').innerHTML='Upload starting...';
if($('UploadProgressBar8')) {
  $('UploadProgressBar8').firstChild.firstChild.style.width='0%'
};
if (document.uploadStatus8) {
  document.uploadStatus8.stop();
}
document.uploadStatus8 =new Ajax.PeriodicalUpdater(
  'UploadStatus8',
  '/hoge/upload_status?upload_id=8',
  Object.extend(
    {
       asynchronous:true,
       evalScripts:true,
       onComplete:function(request) {
         $('UploadStatus8').innerHTML='Upload finished.';
         if($('UploadProgressBar8')) {
           $('UploadProgressBar8').firstChild.firstChild.style.width='100%'
         };
         document.uploadStatus8 = null
       }
     },
     { decay:1.8,frequency:2.0 }
   )
);
return true
">
HTML はこんなかんじ
<iframe id="UploadTarget11" name="UploadTarget11" src="" style="width:0px;height:0px;border:0"></iframe>
<div class="progressBar" id="UploadProgressBar11">
  <div class="border">
    <div class="background">
      <div class="foreground"></div>
    </div>
  </div>
</div>
<div class="uploadStatus" id="UploadStatus11"></div>

すぺじぇね

install
./script/plugin install  http://wota.jp/svn/rails/plugins/branches/stable/special/

事前に position :integer なカラムをつくっておくと、D&Dソート機能が有効になる。

実行
ruby script/generate Model Hogefuga
ruby script/generate special Hogefuga
修正
とりあえずそのままで動く
db/localized/なんちゃら を修正
config/localize.yml とかもある
カラムの表示名を日本語にしたいときは db/localized/softs.yml を編集
表示したくない項目は app/controllers/hoge_controller.rb を編集
/hoge/sort を直接開くと二回目のソートでエラーがでる。/hoge/index から「順序変更」をクリックしないとうまくいかない。

ファイルアップロードフォーム追加

http://www.kanthak.net/opensource/file_column/
http://d.hatena.ne.jp/sweeper/20060210/1139565058
rmagic install
wget http://rubyforge.org/frs/download.php/11316/RMagick-1.13.0.tar.gz
tar -xvzf RMagick-1.13.0.tar.gz
cd RMagick-1.13.0
./configure
make
make install
file_column install
./script/plugin install \
http://opensvn.csie.org/rails_file_column/\
plugins/file_column/trunk
move 。Rails 的には plugin のフォルダ名は見ていないのでそのままでも問題なし。
cd vendor/plugins
mv trunk file_column
app/models/hoge.rb
class Entry < ActiveRecord::Base
  file_column :image
end
uploadフォームを生成する。
file_column_field("picture", "image")
app/views/soft/new.rhtml とかを multipart に変更
<%= start_form_tag({:action => "#{@action_prefix}confirm_create", :id=>@action_id || @belongs_to }, {:multipart => true})%>
uploadした画像を表示する
url_for_file_column("picture", "image")
uploadした画像を表示する
url_for_image_column("picture", "image")
すぺじぇねでファイル名を表示したい場合 to_s はいってないとファイル無しの時エラーになる。
<%=h File.basename(human_attribute_value(soft, column.name).to_s) %>

Lighttpd―Using mod_proxy to do a cluster

install
gem install mongrel_cluster

ajax scaffold

改行コードが CRLF なのがきになるので LF に変更しておく。
app/views/hoge/component.rhtml 全体のテンプレ。青い<h2>の見出しはいってるとこ
app/views/hoge/_ip.rhtml 一行ごとのテンプレ
app/controllers/hoge_controller.rb
表示カラム数の変更
app/controllers/hoge_controller.rb に下記追加
  def default_per_page
    10
  end

ActiveHeart

http://jp.rubyist.net/magazine/?0012-RubyOnRails
./script/plugin install http://svn.rails2u.com/public/plugins/trunk/active_heart/

このページへのコメント

RubyOnRailsPlugin - 鐃術ワ申鐃緒申鐃緒申筌居申鐃緒申个鐃緒申鐃緒申鐃淑どのワ申鐃 - Seesaa Wiki鐃淑ワ申鐃緒申鐃緒申鐃緒申 for 鐃緒申鐃殉¥申鐃夙フワ申鐃緒申 ≪潟若 ≪ http://www.pslcbi.com/moncler2014.html

0
Posted by ≪潟若 ≪ 2013年12月09日(月) 00:51:07 返信

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Wiki内検索

編集にはIDが必要です